简体   繁体   English

调用不同的解析时执行关于promise的顺序

[英]executing order about promise when different resolve is invoked

I'm confused about resolve in Promise. 我对Promise中的resolve感到困惑。

resolve is invoked before innerResolve , but why 'inner Promise then execute' is logged before 'Promise then execute' in chrome console. resolve之前援引innerResolve ,但为什么“无极内再执行”前镀铬控制台“无极然后执行”记录。

I think it might be that: 我认为可能是:

When the state of a promise is PENDING, invoking resolve just set the state to FULFILLED, and when then method is invoked the job will be queued into jobQueue. 当Promise的状态为PENDING时,调用resolve会将状态设置为FULFILLED, then调用方法时,作业将排队到jobQueue中。 innerPromise.then is invoked first, so the job is queued first. innerPromise.then被调用,因此作业首先进入队列。

Is there a normative explanation to this question? 这个问题有规范的解释吗?

Here is my code: 这是我的代码:

console.log("main start")
new Promise(resolve =>{
  new Promise(innerResolve =>{
    resolve()
    console.log("resove is called")
    innerResolve()
    console.log("innerResolve is called")
  }).then(() => {
    console.log('inner Promise then execute')
  })
}).then(() => {
    console.log('Promise then execute');
})
console.log("main end")

resolve() does not invoke then then method. resolve()不调用then方法。 The invocation of resolve() is what queues the then method to be invoked after the next tick, so the entire inner promise resolver will execute at once before the inner then method, and then the outer then method will be invoked in that order on the next tick, because the inner promise is chained chained synchronously inside the outer promise before the outer promise is chained. 在下一个滴答之后,将对then方法进行排队的是resolve()调用,因此整个内部promise解析器将在内部then方法之前立即执行,然后在外部then方法上按该顺序调用下一个刻度,因为内部承诺在外部承诺被链接之前在外部承诺内部被同步链接。

I added two more logs to help clarify why the order of the then methods is different than you were expecting: 我又添加了两个日志来帮助阐明为何then方法的顺序与您期望的不同:

 console.log("main start") new Promise(resolve =>{ console.log("outer resolver execute") new Promise(innerResolve =>{ resolve() console.log("resove is called") innerResolve() console.log("innerResolve is called") }).then(() => { console.log("inner Promise then execute") }) console.log("inner promise chained") }).then(() => { console.log("Promise then execute"); }) console.log("main end") 

bot resolve would be called since they are in a single stack frame 由于它们位于单个堆栈框架中,因此会调用bot resolve

resolve()
console.log("resove is called")
innerResolve()
console.log("innerResolve is called")

once this is done, the event loop checks if the promise is resolved or not and executes all finished promises, since both are done. 一旦完成,事件循环将检查承诺是否已解决,并执行所有已完成的承诺,因为这两个都已完成。 It might call any of them depending on the compiler. 它可能会调用其中的任何一个,具体取决于编译器。 try the following example 试试下面的例子

 console.log("main start") new Promise(resolve =>{ new Promise(innerResolve =>{ resolve() console.log("resove is called") setTimeout(innerResolve, 1000); console.log("innerResolve is called") }).then(() => { console.log('inner Promise then execute') }) }).then(() => { console.log('Promise then execute'); }) console.log("main end") 

since the innerResolve was executed later, the then() was called later 由于innerResolve稍后执行,因此then()稍后被调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM