Promise

js language is a single threaded language

Only one task can be executed at a time, and multiple tasks need to be queued for execution (synchronous mode)

 

 Asynchronous: Each task has one or more callback functions. The callback function is executed after the previous task ends, and the latter task does not need to wait for the previous task to end before it is executed.

1.ajax

2. Event monitoring

f1.on("done",f2)

The above line of code means that when a done event occurs in f1, f2 is executed.

3. Publish/subscribe model (publisht-subscribe)

f2 subscribes jquery to done signal

jQuery.subscribe("done",f2)

Released in f1

 function f1(){

    setTimeout(function () {

      // task code for f1

      jQuery.publish("done");
     }, 1000);
   }

You can also unsubscribe (unsubscribe) after f2 completes execution

jQuery.unsubscribe("done",f2)

4. Promise object

The Promises object is a specification proposed by the CommonJS working group to provide a unified interface for asynchronous programming

Each asynchronous task returns a Promise object, which has a then method, allowing custom callback functions

There can also be multiple callback functions

f1().then(f2).then(f3)

There is also a callback when an error occurs

f1().then(f2).fail(f3)

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324690869&siteId=291194637