-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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: #12442 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: William Kapke <william.kapke@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
- 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); | ||
})); | ||
} |