Skip to main content

State change and callback execution sequence in Promise

 A Promise is simply a container that holds the result of an event (usually an asynchronous operation) that will end in the future. Syntactically, a Promise is an object from which messages of asynchronous operations can be obtained.It has three states: Pending, Resolved, Rejected

Promise objects mainly have the following characteristics:

  • The state of the object is not affected by the outside world: only the result of the asynchronous operation can determine the current state, and no other operation can change this state.
  • Promise cannot be cancelled, it will be executed as soon as it is created, and cannot be cancelled midway.
  • If the callback function is not set, the error thrown inside the Promise will not be reflected to the outside.
  • Once the state changes, it will not change again, and this result can be obtained at any time.
  • When in the Pending state, there is no way to know which stage (just started or about to be completed) is currently progressing.

The basic usage is omitted. The following is the sequence of state change and callback execution in Promise

  let p=new Promise((resolve,reject)=>{
        //    setTimeout(()=>{
            resolve("oK");
        //    },3000);
           console.log("6666")
        });
        p.then(value=>{
            console.log(value);
            console.log("7777")
        })

Summary: The sequence of state change and callback execution in Promise, when the state response is synchronously executed > first change the state and then respond to the callback > when the state is responsively executed asynchronously, execute the callback first to change the state.

The then method returns the determination of the result of the promise object.

  • then does not return a value internally, and subsequent then calls default to undefined.
  • The internal return result of promise is a non-promise object, return number/string/boolean, then the returned promise object is resolved.
  • The return result of then's promise is a promise object, and the state of then's promise object is determined by the internally returned promise object.
  • An error is thrown, then the returned promise object is rejected.


Comments

Popular posts from this blog

What is the difference between the box-size property content-box and border-box in the css box model?

 The box model is a very important concept in CSS layout, it includes content area, padding, border, and margin. Box models can be divided into two types: standard box models and IE box models. The box model, as the name suggests, is used to hold things, and the things it holds are the content of HTML elements. In other words, every visible HTML element is a box.

Js uses recursive way to traverse the dom tree to dynamically create element nodes

 What is a dom tree? In short, DOM is the Document Object Model, which provides a structured representation for documents and defines how to access the document structure through scripts. DOM is composed of nodes. After the HTML is loaded, the rendering engine will generate a DOM tree in memory based on the HTML document. This article uses a small case to traverse the dom tree recursively. The core of the method is to determine whether the incoming data is an array, and then traverse the root node. Note that there must be an end condition when using recursion.