Der*_*ell 2 javascript promise angularjs angular
我有执行此操作的 Angular 1.6 ES6 代码
service.get()
.then((data) => {
console.log('one')
//arbitrary stuff
return service.get()
})
.then((data) => {
console.log('two')
//more stuff
})
.catch((err) => {
//handle err
})
.finally(console.log('finally'))
Run Code Online (Sandbox Code Playgroud)
我想从控制台得到这个:
one
two
finally
Run Code Online (Sandbox Code Playgroud)
但我实际得到的是:
finally
one
two
Run Code Online (Sandbox Code Playgroud)
我如何扭转这种局面,以便在我的承诺链完成之前我的 finally 不会发生?
因为你立即打电话console.log('finally'),
代替:
.finally(console.log('finally'))
Run Code Online (Sandbox Code Playgroud)
和:
.finally(() => console.log('finally'))
Run Code Online (Sandbox Code Playgroud)
编辑
从文档:
Promise.prototype.finally以acallback为参数,当promise完成时,无论是fulfilled还是rejected,都会执行指定的回调函数。