-
Notifications
You must be signed in to change notification settings - Fork 52
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
Makes WrappedPromise an ES6 class #113
Merged
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
module.exports = (Promise, ensureAslWrapper) => { | ||
// Updates to this class should also be applied to the the ES3 version | ||
// in index.js. | ||
return class WrappedPromise extends Promise { | ||
constructor(executor) { | ||
var context, args; | ||
super(wrappedExecutor); | ||
var promise = this; | ||
|
||
try { | ||
executor.apply(context, args); | ||
} catch (err) { | ||
args[1](err); | ||
} | ||
|
||
return promise; | ||
function wrappedExecutor(resolve, reject) { | ||
context = this; | ||
args = [wrappedResolve, wrappedReject]; | ||
|
||
// These wrappers create a function that can be passed a function and an argument to | ||
// call as a continuation from the resolve or reject. | ||
function wrappedResolve(val) { | ||
ensureAslWrapper(promise, false); | ||
return resolve(val); | ||
} | ||
|
||
function wrappedReject(val) { | ||
ensureAslWrapper(promise, false); | ||
return reject(val); | ||
} | ||
} | ||
} | ||
} | ||
}; |
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,11 @@ | ||
'use strict'; | ||
|
||
module.exports = (tap) => { | ||
return class SubclassedPromise extends Promise { | ||
then(onSuccess, onReject) { | ||
tap.type(onSuccess, 'function'); | ||
tap.type(onReject, 'undefined'); | ||
return Promise.prototype.then.call(this, onSuccess, onReject); | ||
} | ||
}; | ||
}; |
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.
I'm not sure if it makes a difference in this case as I'm not that familiar with the new class syntax, but is there a difference between
Promise.prototype.then.call()
andsuper.then()
? I haven't seen the two styles mixed like this beforeThere 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.
I just checked out the branch to test it and the test passed using
super
as well 😃I'm not sure if it's a big deal - if not, just leave it as is and feel free to merge on my account 👍
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 they should be effectively the same. Using super you are guaranteed that your getting tge then you want, using this approach Promise, or Promise.prototype could be changed out from underneath you. In a test this shouldnt be an issue, but would be a bad idea if we were using this in our wrapping code
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.
Thanks for the explanation 👍 I'm fine with just merging this then. We can always modify the style in master if we want