-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
Add promise support to nextTick #3967
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,13 +87,17 @@ export const nextTick = (function () { | |
| } | ||
|
|
||
| return function queueNextTick (cb: Function, ctx?: Object) { | ||
| const func = ctx | ||
| ? function () { cb.call(ctx) } | ||
| : cb | ||
| callbacks.push(func) | ||
| if (!pending) { | ||
| pending = true | ||
| timerFunc() | ||
| if (cb) { | ||
| var func = ctx | ||
| ? function () { cb.call(ctx) } | ||
|
Member
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. Well, if you changed cb to be nullable, then flow will report error here because it is pessimistic |
||
| : cb | ||
| callbacks.push(func) | ||
| if (!pending) { | ||
| pending = true | ||
| timerFunc() | ||
| } | ||
| } else if (typeof Promise !== 'undefined') { | ||
|
Contributor
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. Out of curiosity, rationale for this instead of simply
Contributor
Author
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. With a simple
Member
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.
|
||
| return Promise.resolve(ctx) | ||
| } | ||
| } | ||
| })() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { nextTick } from 'core/util/env' | ||
|
|
||
| describe('nextTick', () => { | ||
| it('accepts a callback', done => { | ||
| nextTick(done) | ||
|
Member
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. Why doesn't this return a promise?
Contributor
Author
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. Because I didn't want to encourage people to mix the two styles (passing a callback vs chaining a promise).
Member
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. The it sounds like separate another API is a better choice?
Contributor
Author
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. I'm not sure I understand what you're saying, but are you suggesting having a separate
Member
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. Yes, exactly
Contributor
Author
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. I'd rather not force people to learn another method name. This convention of being able to use either a callback or a promise is also quite common, whereas I've never seen a library do as you propose. |
||
| }) | ||
|
|
||
| it('returns undefined when passed a callback', () => { | ||
| expect(typeof nextTick(() => {})).toBe('undefined') | ||
| }) | ||
|
|
||
| if (typeof Promise !== 'undefined') { | ||
| it('returns a Promise when provided no callback', done => { | ||
| nextTick().then(done) | ||
| }) | ||
|
|
||
| it('returns a Promise with a context argument when provided a falsy callback and an object', done => { | ||
| const obj = {} | ||
| nextTick(undefined, obj).then(ctx => { | ||
|
Member
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. why ctx is passed as argument, I wonder. Because
Contributor
Author
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. This is for |
||
| expect(ctx).toBe(obj) | ||
| done() | ||
| }) | ||
| }) | ||
| } | ||
| }) | ||
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.
cbis declared asFunction, I would expect it as a non-nullable value. Maybe changing the cb annotation is better.