为什么Promise.resolve()。then()被延迟了? [英] Why Promise.resolve().then() is delayed?

查看:862
本文介绍了为什么Promise.resolve()。then()被延迟了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么resoved Promise延迟 .then()参数调用?



示例:

  var myPromise = Promise.resolve(); 
console.log(myPromise);
myPromise.then(()=> console.log('a'));
console.log('b');

主机回报:

 >承诺{< state>:履行,< value>:undefined} 
> b
> a

如果 myPromise 已满足,为什么 .then()不要调用imediatly解析函数?

解决方案

因为,根据规范,promises会在当前执行线程展开后调用其解析处理程序并返回平台代码。这保证了它们总是被异步调用。



因此,你看到 console.log('b')首先执行该执行线程,然后调用解析处理程序,您将看到 console.log('a')



来自承诺/ A +规范



< blockquote>

2.2.4 onFulfilled或onRejected在执行
上下文堆栈仅包含平台代码之前不得调用。 [3.1]。


并且,这里的注释[3.1]:


这里平台代码表示引擎,环境和承诺
实现代码。实际上,这个要求确保
onFulfilled和onRejected异步执行,之后调用
循环,然后调用新堆栈。这可以是
,可以使用宏任务机制(如setTimeout或
setImmediate)实现,也可以使用微任务机制(如
MutationObserver或process.nextTick)实现。由于promise实现
被认为是平台代码,它本身可能包含一个任务调度
队列或调用处理程序的trampoline。


这样做是为了提供一致的执行顺序,所以无论何时解析(同步或异步), then()处理程序总是在相对于其他代码的相同时间调用。由于许多promise是异步解析的,因此无论如何解决,给定promise的唯一方法就是让它们始终异步调用它们的 .then()处理程序。 / p>

I don't understand why resoved Promise delay .then() argument call?

example:

var myPromise = Promise.resolve();
console.log(myPromise);
myPromise.then(()=>console.log('a'));
console.log('b');

console return:

> Promise { <state>: "fulfilled", <value>: undefined }
> "b"
> "a"

If myPromise is fulfilled, why .then() don't call imediatly resolve function?

解决方案

Because, by specification, promises call their resolve handler AFTER the current thread of execution unwinds and finishes back to "platform code". That guarantees that they are always called asynchronously.

So, thus you see the console.log('b') first as that thread of execution finishes and then the resolve handler is called where you see the console.log('a').

From the Promises/A+ specification:

2.2.4 onFulfilled or onRejected must not be called until the execution context stack contains only platform code. [3.1].

And, here's note [3.1]:

Here "platform code" means engine, environment, and promise implementation code. 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. 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. Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or "trampoline" in which the handlers are called.

This is done to provide consistent execution order so no matter when the promise is resolved (synchronously or asynchronously), the then() handlers are always called in the same timing relative to other code. Since many promises are resolved asynchronously, the only way to make a given promise consistent no matter how it is resolved is to make them always call their .then() handlers asynchronously.

这篇关于为什么Promise.resolve()。then()被延迟了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆