承诺构造函数回调的身体何时执行?
假设我有以下代码构建Promise
:
function doSomethingAsynchronous() {
return new Promise((resolve) => {
const result = doSomeWork();
setTimeout(() => {
resolve(result);
}), 100);
});
}
在哪个时间点为Dosomework()
调用?是在构建 之后还是立即构建?如果没有,我需要明确需要做些其他事情以确保运行回调吗?
Suppose I have the following code constructing a Promise
:
function doSomethingAsynchronous() {
return new Promise((resolve) => {
const result = doSomeWork();
setTimeout(() => {
resolve(result);
}), 100);
});
}
At which point in time is doSomeWork()
called? Is it immediately after or as the Promise
is constructed? If not, is there something additional I need to do explicitly to make sure the callback is run?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,是通过规范同步称为。
来自
这是在ecmascript规范中定义的(当然,很难阅读...)此处(在此编辑时,step  9,表明执行者被同步称为):
(我的重点)
此保证可能很重要,例如,当您准备多个承诺时,您将全部或
race
您的执行者具有同步的副作用。It is called synchronously, yes, by specification.
From the MDN:
This is defined in the ECMAScript specification (of course, it's harder to read...) here (Step 9 as of this edit, showing that the executor is called synchronously):
(my emphasis)
This guarantee may be important, for example when you're preparing several promises you then pass to
all
orrace
, or when your executors have synchronous side effects.您可以通过将同步代码放入体内而不是异步来立即在下面看到身体:
结果显示了立即执行承诺的主体(在打印'B'之前)。
保留承诺的结果,将释放到“然后”调用:
结果:
与身体中的异步代码相同的交易,除了不确定的延迟之前,在实现诺言之前,可以称为“然后”(点)
C
)。因此a
和b
在dosomethingsasynchronous()
返回时,才会立即打印实现(称为“解决”)。一旦添加的调用,在表面上看起来很奇怪,即使在所有内容都同步时,
b
即使在c
之前都打印出来。当然,
a
会打印,然后c
最后b
?a
,b
和c
的原因是,无论身体中的代码是否为async,
或同步
,然后,
方法总是通过Promise
异步调用。在我看来,我想象
然后,
方法被settimeout(()=> {then(pr)},0)
在promise promise promise promise中调用。
一次resolve
被调用。 IE当前的执行路径必须在传递到然后执行
的函数之前完成。从
Promise
规范中不明显。我的猜测是,它可以确保何时调用
然后调用
(始终在当前执行线程完成之后),这大概是允许多个Promises
在踢脚之前被堆叠/链接在一起将所有连续
连续调用。You can see below the body is executed immediately just by putting synchronous code in the body rather than asynchronous:
The result shows the promise body is executed immediately (before 'b' is printed).
The result of the Promise is retained, to be released to a 'then' call for example:
Result:
The same deals with asynchronous code in the body except for the indeterminate delay before the promise is fulfilled and 'then' can be called (point
c
). Soa
andb
would be printed as soon asdoSomethingAsynchronous()
returns butc
appears only when the promise is fulfilled ('resolve' is called).What looks odd on the surface once the call to
then
is added, is thatb
is printed beforec
even when everything is synchronous.Surely
a
would print, thenc
and finallyb
?The reason why
a
,b
, andc
are printed in that order is that no matter whether the code in the body isasync
orsync
, thethen
method is always called asynchronously by thePromise
.In my mind, I imagine the
then
method being invoked by something likesetTimeout(()=>{then(pr)},0)
in thePromise
onceresolve
is called. I.e. the current execution path must be complete before the function passed tothen
will be executed.Not obvious from the
Promise
specification why it does this.My guess is it ensures consistent behavior regarding when
then
is called (always after the current execution thread finishes) which is presumably to allow multiplePromises
to be stacked/chained together before kicking off all thethen
calls in succession.是的,当您构造
Promise
时,第一个参数将立即执行。通常,您不会以您所做的方式真正使用
Promise
,就像当前的实现一样,它仍然是同步的。您宁愿使用超时实现它,或将Resolve函数调用为AJAX回调的一部分,
settimeout
方法将在下一个可能的时刻拨打该函数,事件队列免费Yes, when you construct a
Promise
the first parameter gets executed immediately.In general, you wouldn't really use a
promise
in the way you did, as with your current implementation, it would still be synchronous.You would rather implement it with a timeout, or call the resolve function as part of an ajax callback
The
setTimeout
method would then call the function at the next possible moment the event queue is free来自 ecmascript规范
考虑以下代码:
当我们执行上述代码时,
executorFunction
将在声明Promise
时自动调用,而无需明确调用它。From the EcmaScript specification
Consider the following code:
When we execute the above code,
executorFunction
will be called automatically as soon as we declare thePromise
, without us having to explicitly invoke it.