Home > WeChat Applet > Mini Program Development > WeChat applet es6-promise.js encapsulates requests and handles asynchronous processes

WeChat applet es6-promise.js encapsulates requests and handles asynchronous processes

不言
Release: 2018-06-27 15:59:32
Original
2777 people have browsed it

This article mainly introduces the relevant information about WeChat applet es6-promise.js encapsulating requests and handling asynchronous processes. Friends in need can refer to

WeChat applet es6-promise.js Encapsulating requests and processing asynchronous processes

Download es6-promise.js and place it in the libs folder in the root directory;

Create a new httpsPromisify.js in the utils folder in the root directory. That is, define the method of encapsulating the request

var Promise = require('../libs/es6-promise.min')
function httpsPromisify(fn) { 
 return function (obj = {}) {  
  return new Promise((resolve, reject) => {   
   obj.success = function (res) {    
    resolve(res)   
   }   
   obj.fail = function (res) {    
    reject(res)   
   }   
   fn(obj)  
  }) 
 }
}
module.exports = { 
 httpsPromisify: httpsPromisify
}
  
Copy after login

Call the method:

var Promisify = require('../../utils/httpsPromisify')  Page({

  onLoad: function(){
   Promisify.httpsPromisify(wx.request)({
    url: "https://XXXXXXX",
    header: {
     "Content-Type": "application/x-www-form-urlencoded"
    },
    method: "POST",
    data: {

    }
   }).then(function(res){
     console.log(res)
   })    

  }, })
Copy after login

Note:

There are currently many third-party libraries that support promise, such as $q.js, bluebird.js, etc., but it should be noted that these are not supported in WeChat development tools It can be used normally, but it has no effect on the real machine;

So use es6-Promise.js. This personal test is perfect, and the file size is much smaller than others, so it is recommended for everyone to use it.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to use ECharts to load data asynchronously in WeChat Mini Program

WeChat Mini Program promsie.all and the order of execution of promises

The above is the detailed content of WeChat applet es6-promise.js encapsulates requests and handles asynchronous processes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template