简体   繁体   English

JavaScript中的异步执行顺序

[英]Asynchronous Execution Order in JavaScript

I don't understand why following execution output is 1 3 4 2 6 5 ;我不明白为什么执行 output 后是1 3 4 2 6 5

 Promise.resolve().then(() => console.log(2)); (async() => console.log(await 6))(); console.log(1); (async() => await console.log(3))(); Promise.resolve().then(() => console.log(5)); console.log(4);

Here's how your code executes:以下是您的代码的执行方式:

  1. Promise.resolve() creates a resolved promise which puts () => console.log(2) in the micro-task queue Promise.resolve()创建一个已解析的 promise 将() => console.log(2)放入微任务队列

    queue: [ () => console.log(2) ]
  2. The IIFE executes in which you have await 6 . IIFE 在其中执行await 6 Awaiting 6 is similar to wrapping it in Promise.resolve(6) and then awaiting it: await Promise.resolve(6) .等待 6 类似于将其包装在Promise.resolve(6)中,然后等待它: await Promise.resolve(6)

    When the operand of await is not a promise, await creates a native promise and uses the operand's value to resolve that promiseawait的操作数不是 promise 时, await创建一个原生的 promise 并使用操作数的值来解析 promise

     queue: [ resolve(6), () => console.log(2) ]
  3. 1 is logged on the console 1登录到控制台

    queue: [ resolve(6), () => console.log(2) ] console output: 1
  4. Second IIFE executes where you have await console.log(3) .第二个 IIFE 在您await console.log(3)的地方执行。 This is like await undefined because console.log(3) will execute, 3 will be logged on the console and the return value of console.log , ie undefined will be awaited.这就像await undefined因为console.log(3)将执行, 3将被记录在控制台上并且console.log的返回值,即undefined将被等待。

    In short, you are awaiting undefined .简而言之,您正在等待undefined

     queue: [ resolve(undefined), resolve(6), () => console.log(2) ] console output: 1 3
  5. Promise.resolve() creates a resolved promise which puts () => console.log(5) in the micro-task queue Promise.resolve()创建一个已解析的 promise 将() => console.log(5)放入微任务队列

    queue: [ () => console.log(5), resolve(undefined), resolve(6), () => console.log(2) ] console output: 1 3
  6. 4 is logged on the console 4登录到控制台

    queue: [ () => console.log(5), resolve(undefined), resolve(6), () => console.log(2) ] console output: 1 3 4
  7. Script execution end脚本执行结束

  8. Event loop will now begin processing the micro-task queue事件循环现在将开始处理微任务队列

  9. 2 will be logged on the console because it was queued first 2将在控制台上登录,因为它先排队

    queue: [ () => console.log(5), resolve(undefined), resolve(6) ] console output: 1 3 4 2
  10. await 6 resolves with the value 6 which is then passed to console.log . await 6解析为值6 ,然后传递给console.log As a result 6 is logged on the console结果6被记录在控制台上

    queue: [ () => console.log(5), resolve(undefined) ] console output: 1 3 4 2 6
  11. Promise created as a result of await console.log(3) resolves with the value of undefined Promise 由于await console.log(3)解析为undefined的值

    queue: [ () => console.log(5) ] console output: 1 3 4 2 6
  12. 5 is logged on the console 5登录到控制台

    queue: [ ] console output: 1 3 4 2 6 5
  13. Done.完毕。


Note: Your code shouldn't rely on the timing and order in which different unrelated promises are resolved.注意:您的代码不应依赖于解决不同的不相关承诺的时间和顺序。

There are two sequences here: 1 3 4 and 2 6 5 .这里有两个序列: 1 3 42 6 5 The first is produced on the first tick (before any promises have resolved, and the second on the second tick (after promise resolution).第一个是在第一个 tick 上产生的(在任何 promises 已经解决之前,第二个是在第二个 tick 上(在 promise 决议之后)。

(async() => await console.log(3))(); writes 3 to the log before the await, so the 3 appears before any promises resolve, and between 1 and 4 because of the order of the statements.在 await 之前将3写入日志,因此3出现在任何承诺解决之前,并且由于语句的顺序而出现在 1 和 4 之间。 (Note that the await in this line resolves to undefined , because console.log doesn't return anything). (请注意,此行中的await解析为undefined ,因为 console.log 不返回任何内容)。

For the sequence 2 6 5 , all of the console logs come after a promise resolves (or, equivalently, an await statement), and they're in natural order otherwise.对于序列2 6 5 ,所有控制台日志都出现在 promise 解析之后(或者,等效地,await 语句),否则它们按自然顺序排列。

1 is printed first because of order of execution.由于执行顺序,首先打印 1。 3 is printed as code is executed because console.log() is not an asynchronous operation. 3 在执行代码时打印,因为console.log()不是异步操作。 Does not matter if you put await in front of it.如果你把 await 放在它前面也没关系。 4 is again printed because of order of execution. 4 由于执行顺序再次打印。

Now, rest of the order of execution is due to asynchronous operations.现在,rest的执行顺序是由于异步操作。 The 3 statements are printed in an asynchronous manner after the above 3 statements are executed.以上3条语句执行完毕后,异步打印3条语句。

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

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