-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: expose promise events #21857
Closed
+435
−37
Closed
util: expose promise events #21857
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,37 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const util = require('util'); | ||
|
||
const calls = []; | ||
|
||
const makeHook = (type) => (promise, value) => { | ||
calls.push({ type, promise, value }); | ||
}; | ||
util.createPromiseHook({ | ||
resolve: makeHook('resolve'), | ||
rejectWithNoHandler: makeHook('rejectWithNoHandler'), | ||
handlerAddedAfterReject: makeHook('handlerAddedAfterReject'), | ||
rejectAfterResolved: makeHook('rejectAfterResolved'), | ||
resolveAfterResolved: makeHook('resolveAfterResolved'), | ||
}).enable(); | ||
|
||
const x = (async () => { | ||
throw 'error'; // eslint-disable-line no-throw-literal | ||
})(); | ||
|
||
const check = (item, type, value) => { | ||
assert.strictEqual(item.type, type); | ||
assert.strictEqual(item.value, value); | ||
}; | ||
|
||
process.nextTick(() => { | ||
x.catch(() => {}); | ||
|
||
assert.strictEqual(calls.length, 3); | ||
|
||
check(calls[0], 'resolve', undefined); | ||
check(calls[1], 'rejectWithNoHandler', 'error'); | ||
check(calls[2], 'handlerAddedAfterReject', undefined); | ||
}); |
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,61 @@ | ||
'use strict'; | ||
|
||
const { disableCrashOnUnhandledRejection } = require('../common'); | ||
disableCrashOnUnhandledRejection(); | ||
process.on('unhandledRejection', () => {}); | ||
|
||
This comment was marked as resolved.
Sorry, something went wrong. |
||
const assert = require('assert'); | ||
|
||
const util = require('util'); | ||
|
||
let calls = []; | ||
|
||
const makeHook = (type) => (promise, value) => { | ||
calls.push({ type, promise, value }); | ||
}; | ||
util.createPromiseHook({ | ||
resolve: makeHook('resolve'), | ||
rejectWithNoHandler: makeHook('rejectWithNoHandler'), | ||
handlerAddedAfterReject: makeHook('handlerAddedAfterReject'), | ||
rejectAfterResolved: makeHook('rejectAfterResolved'), | ||
resolveAfterResolved: makeHook('resolveAfterResolved'), | ||
}).enable(); | ||
|
||
const first = Promise.resolve('success'); | ||
let second; | ||
const third = first.then(() => { | ||
second = Promise.reject('error'); | ||
return second; | ||
}); | ||
|
||
|
||
const check = (item, type, promise, value) => { | ||
assert.strictEqual(item.type, type); | ||
assert.strictEqual(item.promise, promise); | ||
assert.strictEqual(item.value, value); | ||
}; | ||
|
||
setTimeout(() => { | ||
const fourth = third.catch(() => {}); | ||
|
||
calls = calls.filter((c) => | ||
[first, second, third, fourth].includes(c.promise)); | ||
|
||
assert.strictEqual(calls.length, 8); | ||
|
||
check(calls[0], 'resolve', first, undefined); | ||
|
||
check(calls[1], 'resolve', second, undefined); | ||
|
||
check(calls[2], 'rejectWithNoHandler', second, 'error'); | ||
|
||
check(calls[3], 'resolve', third, undefined); | ||
|
||
check(calls[4], 'handlerAddedAfterReject', second, undefined); | ||
|
||
check(calls[5], 'resolve', third, undefined); | ||
|
||
check(calls[6], 'rejectWithNoHandler', third, 'error'); | ||
|
||
check(calls[7], 'handlerAddedAfterReject', third, undefined); | ||
}, 10); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here this uses a switch, elsewhere if-elseif statements are used. Can it be consistent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the switch here is used to waterfall behaviour through multiple constants whereas above the if statements are for mapping constants.