-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
n-api: implement promise #14365
Closed
+359
−0
Closed
n-api: implement promise #14365
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
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to test resolution and rejection behavior with promises as @addaleax noted in #14365 (comment). |
||
|
||
// 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah! |
||
'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.
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.
As someone who has helped maintain large promise libraries - I'm totally fine with making people
Promise.resolve
promises they pass tonapi_*
methods.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.
What do you mean by "I'm totally fine making people
Promise.resolve
promises"? Do you mean that you don't mind if the native side resolves a promise created in JS and then passed to the native side?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.
@gabrielschulhof I mean I'm fine with
is_promise
only caring about and working with native promises - and ignoring non-native promises in n-api completely