Small micro-channel application development (xii) Promise will be changed to synchronize asynchronous

// utils/utils.js

  /** 

  * RequestPromise Promise for the rewrite mode wx.request 

  * @Param: {string} myUrl interface address 

  * @Return: Promise instance of an object 

  */ 

const requestPromise myurl = => {
   // return an instance of the object Promise 
  return  new new Promise ((Resolve, Reject) => {
    wx.request({
      url: myUrl,
      success: res => resolve(res)
    })
  })
}

module.exports = {
  requestPromise: requestPromise
}



// Pages / Test / the test.js
 // Import File util.js 
const utilApi the require = ( ' ../../utils/util.js ' )

Page({
  data: {
    myData: ''
  },
  // loadMyData function for the values of the print myData 
  loadMyData () {
    the console.log ( ' acquired data are: ' + the this .data.myData)
  },
  // lifecycle onload function is used to monitor page load 
  onLoad: function () {
    utilApi.requestPromise ( ' http://172.20.10.10:8080/wx?name= OAid = Xiaoming Xiaoming & ' ) 
   // use .then processing result 
  .then (RES => {
        console.log(res.data) 
        this.setData({
          myData: res.data
        })
        console.log(this.data.myData)
        this.loadMyData()
      }) 
}
})

When there are multiple asynchronous requests directly continuously .then (fn) can be processed to clear logic.

Of course, just write a simple function of Promise, is not complete. Promise of a more complete wx.request, and other future business need to improve it. In addition a variety of small development framework also have a ready-made promise of the API, brought the box.

Guess you like

Origin www.cnblogs.com/aaronthon/p/11698201.html