-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Promise is implemented as a pair of objects. `napi_create_promise()` returns both a JavaScript promise and a newly allocated "deferred" in its out-params. The deferred is linked to the promise such that the deferred can be passed to `napi_resolve_deferred()` or `napi_reject_deferred()` to reject/resolve the promise. `napi_is_promise()` can be used to check if a `napi_value` is a native promise - that is, a promise created by the underlying engine, rather than a pure JS implementation of a promise. PR-URL: #14365 Fixes: nodejs/abi-stable-node#242 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
- Loading branch information
1 parent
3f7bdc5
commit 503370e
Showing
7 changed files
with
359 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
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,8 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "test_promise", | ||
"sources": [ "test_promise.c" ] | ||
} | ||
] | ||
} |
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,60 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const test_promise = require(`./build/${common.buildType}/test_promise`); | ||
const assert = require('assert'); | ||
|
||
let expected_result, promise; | ||
|
||
// A resolution | ||
expected_result = 42; | ||
promise = test_promise.createPromise(); | ||
promise.then( | ||
common.mustCall(function(result) { | ||
assert.strictEqual(result, expected_result, | ||
'promise resolved as expected'); | ||
}), | ||
common.mustNotCall()); | ||
test_promise.concludeCurrentPromise(expected_result, true); | ||
|
||
// A rejection | ||
expected_result = 'It\'s not you, it\'s me.'; | ||
promise = test_promise.createPromise(); | ||
promise.then( | ||
common.mustNotCall(), | ||
common.mustCall(function(result) { | ||
assert.strictEqual(result, expected_result, | ||
'promise rejected as expected'); | ||
})); | ||
test_promise.concludeCurrentPromise(expected_result, false); | ||
|
||
// Chaining | ||
promise = test_promise.createPromise(); | ||
promise.then( | ||
common.mustCall(function(result) { | ||
assert.strictEqual(result, 'chained answer', | ||
'resolving with a promise chains properly'); | ||
}), | ||
common.mustNotCall()); | ||
test_promise.concludeCurrentPromise(Promise.resolve('chained answer'), true); | ||
|
||
assert.strictEqual(test_promise.isPromise(promise), true, | ||
'natively created promise is recognized as a promise'); | ||
|
||
assert.strictEqual(test_promise.isPromise(Promise.reject(-1)), true, | ||
'Promise created with JS is recognized as a promise'); | ||
|
||
assert.strictEqual(test_promise.isPromise(2.4), false, | ||
'Number is recognized as not a promise'); | ||
|
||
assert.strictEqual(test_promise.isPromise('I promise!'), false, | ||
'String is recognized as not a promise'); | ||
|
||
assert.strictEqual(test_promise.isPromise(undefined), false, | ||
'undefined is recognized as not a promise'); | ||
|
||
assert.strictEqual(test_promise.isPromise(null), false, | ||
'null is recognized as not a promise'); | ||
|
||
assert.strictEqual(test_promise.isPromise({}), false, | ||
'an object is recognized as not a promise'); |
Oops, something went wrong.