fix nextTick Promise implementation for polyfills

This commit is contained in:
Evan You 2016-11-20 12:03:24 -05:00
parent 6c7bc69328
commit f242e119fa
4 changed files with 27 additions and 14 deletions

View File

@ -86,18 +86,20 @@ export const nextTick = (function () {
}
}
return function queueNextTick (cb: Function, ctx?: Object) {
if (cb) {
var func = ctx
? function () { cb.call(ctx) }
: cb
callbacks.push(func)
if (!pending) {
pending = true
timerFunc()
}
} else if (typeof Promise !== 'undefined') {
return Promise.resolve(ctx)
return function queueNextTick (cb?: Function, ctx?: Object) {
let _resolve
callbacks.push(() => {
if (cb) cb.call(ctx)
if (_resolve) _resolve(ctx)
})
if (!pending) {
pending = true
timerFunc()
}
if (!cb && typeof Promise !== 'undefined') {
return new Promise(resolve => {
_resolve = resolve
})
}
}
})()

View File

@ -119,7 +119,7 @@ describe('Instance methods lifecycle', () => {
}
}).$mount()
vm.msg = 'bar'
vm.$nextTick().then(function (ctx) {
vm.$nextTick().then(ctx => {
expect(ctx).toBe(vm)
expect(vm.$el.textContent).toBe('bar')
done()

View File

@ -1,3 +1,5 @@
require('es6-promise/auto')
// import all helpers
const helpersContext = require.context('../helpers', true)
helpersContext.keys().forEach(helpersContext)

View File

@ -6,7 +6,7 @@ describe('nextTick', () => {
})
it('returns undefined when passed a callback', () => {
expect(typeof nextTick(() => {})).toBe('undefined')
expect(nextTick(() => {})).toBeUndefined()
})
if (typeof Promise !== 'undefined') {
@ -21,5 +21,14 @@ describe('nextTick', () => {
done()
})
})
it('returned Promise should resolve correctly vs callback', done => {
const spy = jasmine.createSpy()
nextTick(spy)
nextTick().then(() => {
expect(spy).toHaveBeenCalled()
done()
})
})
}
})