Skip to content

Commit

Permalink
Simplify info.rejected
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 15, 2019
1 parent 60249ed commit 4a24129
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,8 @@ It is usually an `Error` instance but could be anything.

## `info.rejected`

Boolean indicating whether the promise was resolved or rejected. Only defined
with
[`unhandledRejection`](https://nodejs.org/api/process.html#process_event_unhandledrejection),
[`rejectionHandled`](https://nodejs.org/api/process.html#process_event_rejectionhandled)
and
Boolean indicating whether the promise was initially resolved or rejected. Only
defined with
[`multipleResolves`](https://nodejs.org/api/process.html#process_event_multipleresolves).

## `info.nextValue`, `info.nextRejected`
Expand Down
26 changes: 18 additions & 8 deletions src/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,29 @@ const getInfo = async function({

// Retrieve promise's resolved/rejected state and value.
const parsePromise = async function({ eventName, promise, value }) {
// `uncaughtException` and `warning` events do not have `rejected`.
if (promise === undefined) {
if (NO_PROMISE_EVENTS.includes(eventName)) {
return { value }
}

// `unhandledRejection` should not use the following code because:
// - we already know `rejected` and `value`
// - using `try/catch` will fire `rejectionHandled`
if (eventName === 'unhandledRejection') {
return { rejected: true, value }
const { rejected, value: valueA } = await getPromiseValue({ promise })

// `rejected` is always `true` with `rejectionHandled`, so we skip it
if (eventName === 'rejectionHandled') {
return { value: valueA }
}

// `rejectionHandled` and `multipleResolves` use `await promise`
return { rejected, value: valueA }
}

// Those events do not try to get the promise value.
// For `uncaughtException` and `warning`, they are not promise-specific.
// For `unhandledRejection`:
// - we already know `rejected` and `value`
// - using `try/catch` will fire `rejectionHandled`
const NO_PROMISE_EVENTS = ['uncaughtException', 'warning', 'unhandledRejection']

// `rejectionHandled` and `multipleResolves` otherwise use `await promise`
const getPromiseValue = async function({ promise }) {
try {
return { rejected: false, value: await promise }
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions test/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ const INFOS = [
{
name: 'unhandledRejection',
arg: () => true,
expected: { rejected: true, value: true },
expected: { value: true },
},
{
name: 'rejectionHandled',
arg: () => true,
expected: { rejected: true, value: true },
expected: { value: true },
},
{
name: 'multipleResolves',
Expand Down

0 comments on commit 4a24129

Please sign in to comment.