[Applet] Use uni-app to build applet environment --- encapsulation interface

Promise package

uni-app encapsulates some APIs with promises. The first parameter of the returned data is the error object, and the second parameter is the returned data.

The detailed strategy is as follows:

  • For asynchronous methods, if you do not pass in callback parameters such as success, fail, and complete, the data will be returned as Promise. For example: uni.getImageInfo ()
  • Asynchronous method and return object. If you want to get the return object, you must pass in at least one callback parameter such as success, fail, and complete. For example: uni.connectSocket ()
  • The method of synchronization (that is, ending with sync) does not encapsulate the promise. For example: uni.getSystemInfoSync ()
  • Methods starting with create do not encapsulate promises. For example: uni.createMapContext ()
  • The method that ends with the manager does not encapsulate the promise. For example: uni.getBackgroundAudioManager ()

 

Examples of use:

// 默认方式
uni.request({ url: 'https://www.example.com/request', success: (res) => { console.log(res.data); } }); // Promise uni.request({ url: 'https://www.example.com/request' }) .then(data => {//data为一个数组,数组第一项为错误信息,第二项为返回数据 var [error, res] = data; console.log(res.data); }) // Await function async request () { var [error, res] = await uni.request({ url: 'https://www.example.com/request' }); console.log(res.data); }

 

Relevant information:

Guess you like

Origin www.cnblogs.com/websmile/p/11585451.html