Small micro-channel program: instead of callback hell with Promise solutions. Repair this._invokeMethod is not a function of the problem

/ * * 
 * Callback hell converted to form Promise 
 * https://blog.csdn.net/SEAYEHIN/article/details/88663740 
 * RAW: 
    wx.downloadFile ({ 
        URL: this.data.curImg, 
        Success: RES => { 
            the console.log (20,191,121,213,856, RES) 
        } 
    }) 

  now: 
    the async Go () { 
        const = this.app.pm the downloadFile (wx.downloadFile) 
        const the await the downloadFile RES = ({URL: this.data.curImg}) 
        Console. log (20,191,121,212,742, RES) 
    } 

  fixbug: "this._invokeMethod is not a function" - with .bind (ctx) way to solve 

  if wx is a built-in function, can be used directly, but some API is an example API, such as 
   
  this. ctx = wx.createCameraContext ()
  this. ctx.takePhoto 

  If you use this way of development, then, must the aforementioned problems.
  this.app.pm takePhoto = const (this.ctx.takePhoto) 
  the await takePhoto () // this._invokeMethod IS A function not 

  reason is simple, when execution context is not the instance itself, so we gave it to. 
  this.app.pm takePhoto = const (this.ctx.takePhoto.bind (this.ctx)) 
  the await takePhoto () 
 * / 
const PM = API => (Options, ... params) => new new Promise ((Resolve, reject) => api ({... options, success: resolve, fail: reject}, ... params))

 

Guess you like

Origin www.cnblogs.com/CyLee/p/11909038.html