-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds the `multipleResolves` event to track promises that resolve more than once or that reject after resolving. It is important to expose this to the user to make sure the application runs as expected. Without such warnings it would be very hard to debug these situations. PR-URL: #22218 Fixes: nodejs/promises-debugging#8 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]>
- Loading branch information
Showing
6 changed files
with
177 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const rejection = new Error('Swallowed reject'); | ||
const rejection2 = new TypeError('Weird'); | ||
const resolveMessage = 'First call'; | ||
const rejectPromise = new Promise((r) => setTimeout(r, 10, rejection2)); | ||
const swallowedResolve = 'Swallowed resolve'; | ||
const swallowedResolve2 = 'Foobar'; | ||
|
||
process.on('multipleResolves', common.mustCall(handler, 4)); | ||
|
||
const p1 = new Promise((resolve, reject) => { | ||
resolve(resolveMessage); | ||
resolve(swallowedResolve); | ||
reject(rejection); | ||
}); | ||
|
||
const p2 = new Promise((resolve, reject) => { | ||
reject(rejectPromise); | ||
resolve(swallowedResolve2); | ||
reject(rejection2); | ||
}).catch(common.mustCall((exception) => { | ||
assert.strictEqual(exception, rejectPromise); | ||
})); | ||
|
||
const expected = [ | ||
'resolve', | ||
p1, | ||
swallowedResolve, | ||
'reject', | ||
p1, | ||
rejection, | ||
'resolve', | ||
p2, | ||
swallowedResolve2, | ||
'reject', | ||
p2, | ||
rejection2 | ||
]; | ||
|
||
let count = 0; | ||
|
||
function handler(type, promise, reason) { | ||
assert.strictEqual(type, expected.shift()); | ||
// In the first two cases the promise is identical because it's not delayed. | ||
// The other two cases are not identical, because the `promise` is caught in a | ||
// state when it has no knowledge about the `.catch()` handler that is | ||
// attached to it right afterwards. | ||
if (count++ < 2) { | ||
assert.strictEqual(promise, expected.shift()); | ||
} else { | ||
assert.notStrictEqual(promise, expected.shift()); | ||
} | ||
assert.strictEqual(reason, expected.shift()); | ||
} |