aws / aws/aws-appsync-community

odd behavior in js-resolver with nullish coalescing operator ?? and a function call on RHS

Open
#403 3 comments 1 reaction 0 assignees View on GitHub
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

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.