javascript – 当调用Promise.then()钩子时?
作者:互联网
我观察到在Firefox的承诺中推迟完整性通知.断言失败后,因为onFullfilled()调用太晚了*.
var resolved = false;
function onFullfilled() {
resolved = true;
log("Completed");
}
Promise.resolve(true).then(onFullfilled);
assert(resolved, "Promise completed promise should call resolution hook immediately.");
当确保onFullfilled()保证在Promise解析时被调用?
*在我的情况下,在测试框架报告断言失败后出现“已完成”日志消息.
解决方法:
执行所有同步代码后,始终会调用Promise resolution钩子.这是设计 – 并且是为了防止竞争条件.
由于promises有时会异步解析规范,因此它们总是异步解析,因此执行相同的代码路径.承诺守护你against Zalgo.
onFulfilled or onRejected must not be called until the execution context stack contains only platform code.
许多测试框架 – 即Mocha支持测试直接承诺 – 承诺语法 – 通过返回一个promise.
it("does something", function(){
return aPromise; // if aPromise resolves the test passes
})
标签:javascript,promise,es6-promise 来源: https://codeday.me/bug/20191007/1865621.html