Skip to content

Commit f242e11

Browse files
committed
fix nextTick Promise implementation for polyfills
1 parent 6c7bc69 commit f242e11

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

src/core/util/env.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,20 @@ export const nextTick = (function () {
8686
}
8787
}
8888

89-
return function queueNextTick (cb: Function, ctx?: Object) {
90-
if (cb) {
91-
var func = ctx
92-
? function () { cb.call(ctx) }
93-
: cb
94-
callbacks.push(func)
95-
if (!pending) {
96-
pending = true
97-
timerFunc()
98-
}
99-
} else if (typeof Promise !== 'undefined') {
100-
return Promise.resolve(ctx)
89+
return function queueNextTick (cb?: Function, ctx?: Object) {
90+
let _resolve
91+
callbacks.push(() => {
92+
if (cb) cb.call(ctx)
93+
if (_resolve) _resolve(ctx)
94+
})
95+
if (!pending) {
96+
pending = true
97+
timerFunc()
98+
}
99+
if (!cb && typeof Promise !== 'undefined') {
100+
return new Promise(resolve => {
101+
_resolve = resolve
102+
})
101103
}
102104
}
103105
})()

test/unit/features/instance/methods-lifecycle.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe('Instance methods lifecycle', () => {
119119
}
120120
}).$mount()
121121
vm.msg = 'bar'
122-
vm.$nextTick().then(function (ctx) {
122+
vm.$nextTick().then(ctx => {
123123
expect(ctx).toBe(vm)
124124
expect(vm.$el.textContent).toBe('bar')
125125
done()

test/unit/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require('es6-promise/auto')
2+
13
// import all helpers
24
const helpersContext = require.context('../helpers', true)
35
helpersContext.keys().forEach(helpersContext)

test/unit/modules/util/next-tick.spec.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('nextTick', () => {
66
})
77

88
it('returns undefined when passed a callback', () => {
9-
expect(typeof nextTick(() => {})).toBe('undefined')
9+
expect(nextTick(() => {})).toBeUndefined()
1010
})
1111

1212
if (typeof Promise !== 'undefined') {
@@ -21,5 +21,14 @@ describe('nextTick', () => {
2121
done()
2222
})
2323
})
24+
25+
it('returned Promise should resolve correctly vs callback', done => {
26+
const spy = jasmine.createSpy()
27+
nextTick(spy)
28+
nextTick().then(() => {
29+
expect(spy).toHaveBeenCalled()
30+
done()
31+
})
32+
})
2433
}
2534
})

0 commit comments

Comments
 (0)