何时承诺。
因此,我是诺言的新手,我正在尝试一些事情来找出引擎盖下发生的事情。从我读到的工作中,在承诺将方法链接到解决方案之后,立即排队到Microtask队列。
Promise.resolve("FIRST")
.then((m) => console.log(m))
.then(() => console.log("FIRST on second level"))
.then(() => console.log("FIRST on third level"))
.then(() => console.log("FIRST on fourth level"));
Promise.resolve("SECOND")
.then((m) => console.log(m))
.then(() => console.log("SECOND on second level"))
.then(() => console.log("SECOND on third level"))
.then(() => console.log("SECOND on fourth level"));
Promise.all([
Promise.resolve("Middle 1 resolved"),
Promise.resolve("Middle 2 resolved"),
]).then((results) => console.log(results));
好吧,第一个链的第一个。然后立即排队。第二链的第一个也是如此。但是他们俩都返回承诺,直到执行前两个排队的作业为止。那么,为什么会这样的日志:
FIRST
SECOND
FIRST on second level
SECOND on second level
[ 'Middle 1 resolved', 'Middle 2 resolved' ]
FIRST on third level
SECOND on third level
FIRST on fourth level
SECOND on fourth level
为什么“中间”日志不放置在“第二级”之前? 我从我读到的内容中假设承诺从承诺中恢复了。在本示例中只要堆栈是空的,因此它将在前两个之后立即排队。
then(m => console.log(m)) //on line 2 and line 8
因为 我在哪里错了?我缺少或不正确理解什么? 先感谢您!
So i am new to promises and i am trying some stuff to figure out what's going on under the hood. From what i read a Job is queued to the Microtask queue by .then immediately after the promise the method was chained to resolves.
Promise.resolve("FIRST")
.then((m) => console.log(m))
.then(() => console.log("FIRST on second level"))
.then(() => console.log("FIRST on third level"))
.then(() => console.log("FIRST on fourth level"));
Promise.resolve("SECOND")
.then((m) => console.log(m))
.then(() => console.log("SECOND on second level"))
.then(() => console.log("SECOND on third level"))
.then(() => console.log("SECOND on fourth level"));
Promise.all([
Promise.resolve("Middle 1 resolved"),
Promise.resolve("Middle 2 resolved"),
]).then((results) => console.log(results));
So ok, the first .then of the FIRST chain is queued immediately. The same goes for the first of the SECOND chain. But they both return promises which will be pending until the first two queued jobs get executed. So why is the log like this:
FIRST
SECOND
FIRST on second level
SECOND on second level
[ 'Middle 1 resolved', 'Middle 2 resolved' ]
FIRST on third level
SECOND on third level
FIRST on fourth level
SECOND on fourth level
Why isn't the "middle" log placed before "FIRST on second level"? Because i was assuming from what i read that the promise returned from Promise.all IN THIS EXAMPLE will resolve as long as the stack is empty and therefore its .then will get queued right after the first two:
then(m => console.log(m)) //on line 2 and line 8
So where am i wrong? What am i missing or don't understand properly?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Promise.All
创建一个新的承诺,并要知道何时解决该承诺,它需要在您传递的每个承诺上调用。因此,首先,数组中的承诺必须解决,然后由Promise创造的承诺可以解决,然后才能调用您的控制台。
换句话说,有2个级别的调用
。只是一个级别隐藏在
promise.ly.promist.ly
的实现中,如果有帮助,这是Promise.all在做什么的近似值:
Promise.all
creates a new promise, and for it to know when to resolve that promise, it needs to call.then
on each of the promises you pass into it. So first the promises in the array must resolve, then the promise created by Promise.all can resolve, and only then do does your console.log get called.In other words, there are 2 levels of calls to
.then
, much like your other second level examples. It's just that one level is hidden inside the implementation ofPromise.all
If it helps, here's an approximation of what Promise.all is doing: