aws / aws/aws-appsync-community
odd behavior in js-resolver with nullish coalescing operator ?? and a function call on RHS
- Dominant language
- HTML
- Stars
- 507
- Forks
- 37
- PR merge metrics
- No merged PRs in 30d
Description
Given the following:
```javascript
const result = a ?? b();
```
The expected behavior in [standard JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing) is that `b()` is not invoked if `a` is non-null and non-undefined.
The behavior I'm observing is that `b()` is invoked, but the assignment to `result` is the value of `a`.
This suggests to me that both the LHS and RHS are being evaluated, but the assignment is from the LHS since it is truthy.
My workaround for this is to use the ternary operator which correctly short-circuits.
Contributor guide
Research direction
Start with the js-resolver evaluation path for the nullish coalescing operator and compare it with the ternary-operator behavior described in the report. Reproduce `const result = a ?? b()` with a non-null `a`; done means `b()` is not invoked and `result` still receives `a`.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100