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 }
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) }) }, })
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!