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

favicon.ico 404 Not Found Causes and Solutions

 What is favicon? It is the abbreviation of Favorites Icon. Its function is that in addition to displaying the corresponding title in the browser's favorites, icons can also be used to distinguish different websites. When I encounter this kind of error, I feel very uncomfortable. There is obviously no problem. Why is a 404 error reported? The following is the solution.

Use of custom events in Vue components.

 The parent component uses props to pass data (such as a method) to the child component, and the child component needs to use custom events to pass data back to the parent component! We can use v-on to bind custom events, and then pass them to subcomponents, which will call after receiving the method. Each Vue instance implements the Events interface, that is: use $on(eventName) to listen for events and $emit(eventName) to trigger events.

mapState, mapGetters, mapMutations, mapActions in vuex

 The above four objects are all in the vuex plug-in store, which is often used to store some data shared globally. But I still feel a little uncomfortable when using it, that is, I wrote a lot of repeated prefixes. At this time, these four objects appeared, which solved this pain point very well. mapState is mainly used to manipulate and obtain data, mapGetters is equivalent to computed properties, and mapMutations and mapActions are simplifications of mutation and action. They can be written as both objects and arrays.