Home > Web Front-end > JS Tutorial > body text

What Is the Order of Execution in JavaScript Promises?

Patricia Arquette
Release: 2024-10-24 11:22:02
Original
935 people have browsed it

What Is the Order of Execution in JavaScript Promises?

What is the order of execution in JavaScript promises?

JavaScript promises are a way to handle asynchronous operations. When a promise is resolved, it executes its .then() handlers asynchronously after the current thread of execution finishes. This means that any synchronous code in the current thread will run before the .then() handlers.

Example

Consider the following code snippet:

<code class="javascript">Promise.resolve('A')
  .then(function(a){console.log(2, a); return 'B';})
  .then(function(a){
     Promise.resolve('C')
       .then(function(a){console.log(7, a);})
       .then(function(a){console.log(8, a);});
     console.log(3, a);
     return a;})
  .then(function(a){
     Promise.resolve('D')
       .then(function(a){console.log(9, a);})
       .then(function(a){console.log(10, a);});
     console.log(4, a);})
  .then(function(a){
     console.log(5, a);});
console.log(1);
setTimeout(function(){console.log(6)},0);</code>
Copy after login

The result of this code snippet is:

<code class="text">1
2 "A"
3 "B"
7 "C"
4 "B"
8 undefined
9 "D"
5 undefined
10 undefined
6</code>
Copy after login

Explanation

  1. Line 1: The code starts by resolving a promise with the value 'A'.
  2. Lines 2-4: The .then() handler on this promise logs the value 'A' to the console and returns the value 'B'.
  3. Lines 5-9: The .then() handler on the promise returned by the previous handler logs the value 'B' to the console and returns the same value.
  4. Lines 10-14: The .then() handler on the promise returned by the previous handler logs the value 'B' to the console and returns the same value.
  5. Lines 15-19: The .then() handler on the promise returned by the previous handler logs the value 'B' to the console and returns the same value.
  6. Line 20: The .then() handler on the promise returned by the previous handler logs the value 'B' to the console.
  7. Line 21: A setTimeout() function is used to schedule a function to be executed after a delay of 0 milliseconds.
  8. Line 22: A console.log() function is used to log the value '1' to the console.

Order of Execution

The order of execution in this code snippet is:

  1. The console.log() on line 22 is executed first, followed by the Promise.resolve() on line 1.
  2. The .then() handler on line 2 is executed next, followed by the .then() handler on line 5.
  3. The .then() handler on line 10 is executed next, followed by the .then() handler on line 15.
  4. The .then() handler on line 20 is executed next.
  5. The setTimeout() function on line 21 is executed last.

The above is the detailed content of What Is the Order of Execution in JavaScript Promises?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template