-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Resolver construct to simplify promise/resolve/reject situations #1256
Conversation
/** | ||
* @template T | ||
*/ | ||
class Resolver { |
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.
can we rename this to Deferred
as its close to the jquery Deferred object. (where its an instance that has a .promise property and .resolve/.reject methods). i dont feel strongly either way so feel free to ignore.
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.
But it's "Resolver" in Closure and YUL.
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.
sgtm.
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 agree that this should be Deferred, that's what all JS libraries call it.
Tests failed. Automated cross-browser testing via Sauce Labs and Travis CI shows that the JavaScript changes in this pull request are: BUSTED Commit: b18ab6e (Please note that this is a fully automated comment.) |
/** @const @type {!Promise<T>} */ | ||
this.promise = promise; | ||
/** @const @type {function(T)} */ | ||
this.resolve = resolve.bind(null); |
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.
Is there a reason to bind here? I don't know of a promise implementation that changes behavior due the to this
.
Just note that Bluebird considers this an anti-pattern. |
It's an anti pattern if overused. Promise's API not supporting this by
|
The real antipattern is the leaking of resolver function itself. In some cases Resolver would make it better. That being said, I haven't yet bought into this idea either. I'll first take a look at how often we store the resolvers as instance vars. I did see recently an increase in those where we store pairs of promise/resolver. It's possible that those can be changed. If I go ahead with this, I will put it into presubmit restriction set. |
So, I looked through most of cases we have today, and my opinion is that in most of cases, it's possible to refactor code to avoid resolver leaker. So, I'm gonna close this PR. |
No description provided.