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 width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.