Skip to content
This repository was archived by the owner on Jan 1, 2025. It is now read-only.

Avoid unhandled promise rejection in callbacks - #2075

Closed
impl wants to merge 1 commit into
facebookexperimental:mainfrom
impl:fix-callback-unhandled-promise-rejection
Closed

impl wants to merge 1 commit into
facebookexperimental:mainfrom
impl:fix-callback-unhandled-promise-rejection

Conversation

@impl

@impl impl commented Oct 20, 2022 •

Copy link
Copy Markdown
Contributor

The creation of a finally branch of the promise in useRecoilCallback causes unhandled promise rejections, despite the fact that the promise is actually handled downstream. This change returns the finally branch instead of the original promise, so there's no divergence in the handling.

For example, the following selector currently "throws" twice, once caught/logged and once not caught:

const testState = selector({
  key: 'testState',
  get: ({ getCallback }) =>
    getCallback(() => async () => {
      throw 'test';
    }),
});

function Component() {
  const test = useRecoilValue(testState);

  useEffect(() => {
    test().then(
      () => console.log('OK'),
      (error) => console.log(error),
    );
  }, [test]);

  // ...
}

The creation of a finally branch of the promise causes unhandled promise
rejections, despite the fact that the promise is actually handled
downstream. This change returns the finally branch instead of the
original promise, so there's no divergence in the handling.
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 20, 2022
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@wd-fb has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

snipershooter0701 pushed a commit to snipershooter0701/Recoil that referenced this pull request Mar 5, 2023
Summary:
The creation of a finally branch of the promise in `useRecoilCallback` causes unhandled promise rejections, despite the fact that the promise is actually handled downstream. This change returns the finally branch instead of the original promise, so there's no divergence in the handling.

For example, the following selector currently "throws" twice, once caught/logged and once not caught:

```javascript
const testState = selector({
  key: 'testState',
  get: ({ getCallback }) =>
    getCallback(() => async () => {
      throw 'test';
    }),
});

function Component() {
  const test = useRecoilValue(testState);

  useEffect(() => {
    test().then(
      () => console.log('OK'),
      (error) => console.log(error),
    );
  }, [test]);

  // ...
}
```

Pull Request resolved: facebookexperimental/Recoil#2075

Reviewed By: drarmstr

Differential Revision: D40573295

Pulled By: wd-fb

fbshipit-source-id: 2683fbb2d881a61ab800c114d0c72997aa25dd3a
@impl
impl deleted the fix-callback-unhandled-promise-rejection branch May 14, 2023 05:46
eagle2722 added a commit to eagle2722/Recoil that referenced this pull request Sep 21, 2024
Summary:
The creation of a finally branch of the promise in `useRecoilCallback` causes unhandled promise rejections, despite the fact that the promise is actually handled downstream. This change returns the finally branch instead of the original promise, so there's no divergence in the handling.

For example, the following selector currently "throws" twice, once caught/logged and once not caught:

```javascript
const testState = selector({
  key: 'testState',
  get: ({ getCallback }) =>
    getCallback(() => async () => {
      throw 'test';
    }),
});

function Component() {
  const test = useRecoilValue(testState);

  useEffect(() => {
    test().then(
      () => console.log('OK'),
      (error) => console.log(error),
    );
  }, [test]);

  // ...
}
```

Pull Request resolved: facebookexperimental/Recoil#2075

Reviewed By: drarmstr

Differential Revision: D40573295

Pulled By: wd-fb

fbshipit-source-id: 2683fbb2d881a61ab800c114d0c72997aa25dd3a
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants