Get the App
SLTechnology News&Howtos  ›  Development  › 

How does WeChat Mini Programs use es6-promise.js to encapsulate requests and handle asynchronous processes

Shulou Source: shulou.com Published: 2022-06-01 02:05:23 09月22日 Update

Most people do not understand the knowledge points of this article "how to use es6-promise.js to encapsulate requests and handle asynchronous processes", so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has certain reference value. I hope you can get something after reading this article. Let's take a look at this article entitled "how WeChat Mini Programs uses es6-promise.js to encapsulate requests and handle asynchronous processes".

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

Download es6-promise.js under the libs folder in the root directory

Create a new httpsPromisify.js under the root directory utils folder, that is, define the method to encapsulate 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 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:

At present, there are many third-party libraries that support promise, such as $q.jsdybluebird.js, etc., but it should be noted that these can be used normally on Wechat development tools, but there is no effect on the real machine; so with es6-Promise.js, this personal test is perfect, and the file size is much smaller than the others, so it is recommended that you use it.

The above is about the content of this article on "how WeChat Mini Programs uses es6-promise.js to encapsulate requests and handle asynchronous processes". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Tags: Encapsulation content programs processes processes files folders articles methods root directories knowledge articles values sizes most that is tools suggestions development tools effects Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Huawei macOS Shulou Information Microsoft