Pure callback small program of callbacks, promise and async, await differences and usage

CallBack difference between pure and promise

1, purely because callBack callback callback deprived of the ability to function, so when a lot of time calling the function of the number of layers, the layers need to pass callBack

2, promise not require transfer layers callBack, because it has the ability to function so that the return

3, for the case of multiple asynchronous need to merge with a pure callback is rather cumbersome, but promise good solution to this problem

4, promise an object is, different from the function to preserve the state of the object, and the called function returns the status needed immediately (except for the closure function)

 

Basic usage of promise

1, new object is a promise

2, asynchronous code function written in the promise

3, promise to accept two parameters, a resolve (already successful), a reject (failed)

4, promise three states pendding (in progress, when the promise is pendding new state), fulfilled (been successful), rejected (failed), when a successful call to resolve the status to have been successful, when failure status to reject the call has failed, once the state changes, the state on the solidification, behind you can not change the status, success will be the success of the return data, failure information will fail to return.

Gets 5, where the need to obtain data through promise.then () way, which accepts two parameters which are anonymous function, the first function is to accept success, and the second is a function of the time of failure

Promise = const new new Promise ((Resolve, Reject) => { 
  wx.getSystemInfo ({ 
    Success: (RES) => { 
      Resolve (RES) 
    }, 
    Fail: (error) => { 
      Reject (error) 
    } 
  }) 
}) ; 

promise.then ((RES) => {
   // obtain successful results, res stored in the data acquisition success 
  console.log (RES) 
}, (error) => {
   // get data fails 
  console.log (error) 
})

6, es6 wording, if the function only one parameter, it does not need parentheses, if only one line of code in brackets, braces may not be required

promise.then ((RES) => {
   // obtain successful results, res data acquired in deposit with success 
  console.log (RES) 
}, (error) => {
   // get data fails 
  console.log ( error) 
}) 

const promise1 = new new Promise ((Resolve, Reject) => { 
  wx.getSystemInfo ({ 
    Success: RES => Resolve (RES), 
    Fail: error => Reject (error) 
  }) 
}); 

promise1.then ( 
  RES => the console.log (RES), 
  error => the console.log (error) 
)

 

promise more than the combined use of asynchronous tasks (promise.all promise.race)

promise.all is more than all the others returned to the callback request

promise.race as long as there is a plurality of requests are returned to the callback request is completed, it returns the result of the first callback

the onLoad: function (Options) { 
    wx.showLoading (); 
    const Bid = options.bid 
    const Detail = bookModel.getDetail (Bid); 
    const Comments = bookModel.getComments (Bid); 
    const likeStatus = bookModel.getLikeStatus (Bid)
     // Promise.all should all return and all requests to the callback there is a .race long as there is a sub-request completion of a callback, the callback is the result of competitive success callback is completed 
    Promise.all ([the Detail, Comments, likeStatus]) 
    . the then (RES => {
       the this .setData ({ 
        Book: RES [ 0 ], 
        Comments: RES [ . 1 ] .comments, 
        likeStatus: RES [2].like_status,
        likeCount: res[2].fav_nums
      })
      wx.hideLoading()
    })
  }

 

async, await the basis of usage

1, async and await an asynchronous request so that the operation becomes "synchronization", which enables asynchronous and synchronous as a result of direct return returned success or failure, the data acquired from the variable to hold the result of the determination, and a direct result obtaining

2, async and await appear in pairs, no one left, the other one will not survive

3, using the async and await is based on promise, and also returns a promise async objects

timeout(ms){
    return new Promise(resolve=>{
      setTimeout(resolve, ms)
    })
  }

  async asyncPrint(){
    await this.timeout(123)
    console.log(11)
  }

 

Guess you like

Origin www.cnblogs.com/dgxblogs/p/11492030.html