forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: add internal bindings for promise handling
Add methods for creating, resolving and rejecting promises using the V8 C++ API that does not require creation of extra `resolve` and `reject` functions to `process.binding('util')`. PR-URL: nodejs#12442 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: William Kapke <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Teddy Katz <[email protected]>
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { | ||
createPromise, promiseResolve, promiseReject | ||
} = process.binding('util'); | ||
const { inspect } = require('util'); | ||
|
||
common.crashOnUnhandledRejection(); | ||
|
||
{ | ||
const promise = createPromise(); | ||
assert.strictEqual(inspect(promise), 'Promise { <pending> }'); | ||
promiseResolve(promise, 42); | ||
assert.strictEqual(inspect(promise), 'Promise { 42 }'); | ||
promise.then(common.mustCall((value) => { | ||
assert.strictEqual(value, 42); | ||
})); | ||
} | ||
|
||
{ | ||
const promise = createPromise(); | ||
const error = new Error('foobar'); | ||
promiseReject(promise, error); | ||
assert(inspect(promise).includes('<rejected> Error: foobar')); | ||
promise.catch(common.mustCall((value) => { | ||
assert.strictEqual(value, error); | ||
})); | ||
} | ||
|
||
{ | ||
const promise = createPromise(); | ||
const error = new Error('foobar'); | ||
promiseReject(promise, error); | ||
assert(inspect(promise).includes('<rejected> Error: foobar')); | ||
promiseResolve(promise, 42); | ||
assert(inspect(promise).includes('<rejected> Error: foobar')); | ||
promise.catch(common.mustCall((value) => { | ||
assert.strictEqual(value, error); | ||
})); | ||
} |