[英]Why does my function execute before my promise callback?
Why does a function called after my promise execute before the promise's callback?为什么在我的承诺之后调用的函数在承诺的回调之前执行?
I read this in MDN, but didn't understand it我在 MDN 上读过这个,但没看懂
"Callbacks will never be called before the completion of the current run of the JavaScript event loop."
“在 JavaScript 事件循环的当前运行完成之前,永远不会调用回调。”
I thought it meant that if I have any other statements after resolve()
or reject()
they will get executed before the callback is invoked.我认为这意味着如果我在
resolve()
或reject()
之后有任何其他语句,它们将在调用回调之前执行。 Though, that seems to be an incomplete understanding.虽然,这似乎是一个不完整的理解。
function myFunction() {
return new Promise( function(resolve, reject) {
const err = false;
if(err) {
reject("Something went wrong!!!");
}
else {
resolve("All good");
}
});
}
myFunction().then(doSuccess).catch(doError);
doOther();
function doError(err) {
console.log(err);
}
function doSuccess() {
console.log('Success');
}
function doOther() {
console.log("My Other Function");
}
Output:输出:
My Other Function我的其他功能
Success成功
By specification, a promise .then()
or .catch()
callback is never called synchronously, but is called on a future tick of the event loop.根据规范,promise
.catch()
.then()
或.catch()
回调永远不会同步调用,而是在事件循环的未来滴答上调用。 That means that the rest of your synchronous code always runs before any .then()
handler is called.这意味着您的同步代码的其余部分始终在调用任何
.then()
处理程序之前运行。
So, thus your doOther()
function runs before either doSuccess()
or doError()
are called.因此,您的
doOther()
函数在doSuccess()
或doError()
之前运行。
Promises are designed this way so that a promise .then()
handler will be called with consistent timing whether the promise is resolved immediately or resolved some time in the future. Promise 是这样设计的,因此无论 Promise 是立即解决还是在未来某个时间解决,promise
.then()
处理程序都会以一致的时间被调用。 If synchronous .then()
handlers were allowed, then calling code would either have to know when it might get called synchronously or you'd be susceptible to weird timing bugs.如果允许同步
.then()
处理程序,那么调用代码要么必须知道它何时可能被同步调用,要么您很容易受到奇怪的计时错误的影响。
In the Promises/A+ specification which the promises in the ES6 specification were based on, it defines a `.then() handler like this:在 ES6 规范中的 Promise 所基于的Promises/A+ 规范中,它定义了一个 `.then() 处理程序,如下所示:
promise.then(onFulfilled, onRejected)
and then has this to say about it:然后有话要说:
2.2.4.
2.2.4. onFulfilled or onRejected must not be called until the execution context stack contains only platform code.
在执行上下文堆栈仅包含平台代码之前,不得调用 onFulfilled 或 onRejected。 [3.1].
[3.1]。
And, then it defines platform code like this:然后它定义了这样的平台代码:
Here “platform code” means engine, environment, and promise implementation code.
这里的“平台代码”是指引擎、环境和promise实现代码。 In practice, this requirement ensures that onFulfilled and onRejected execute asynchronously, after the event loop turn in which then is called, and with a fresh stack.
在实践中,这个要求确保 onFulfilled 和 onRejected 异步执行,在调用 then 的事件循环之后,并使用新的堆栈。 This can be implemented with either a “macro-task” mechanism such as setTimeout or setImmediate, or with a “micro-task” mechanism such as MutationObserver or process.nextTick.
这可以通过“宏任务”机制(例如 setTimeout 或 setImmediate)或“微任务”机制(例如 MutationObserver 或 process.nextTick)来实现。 Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or “trampoline” in which the handlers are called.
由于promise实现被认为是平台代码,它本身可能包含一个任务调度队列或“trampoline”,在其中调用处理程序。
Basically what this means is that .then()
handlers are called by inserting a task in the event loop that will not execute until the currently running Javascript finishes and returns control back to the interpreter (where it can retrieve the next event).基本上这意味着
.then()
处理程序通过在事件循环中插入一个任务来调用,该任务在当前运行的 Javascript 完成并将控制返回给解释器(它可以检索下一个事件)之前不会执行。 So, thus any synchronous Javascript code you have after the .then()
handler is installed will always run before the .then()
handler is called.因此,安装
.then()
处理程序之后的任何同步 Javascript 代码将始终在调用.then()
处理程序之前运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.