This time I will bring you the combined use of wx.request and Promise in the WeChat applet. What are the precautions for the combined use of wx.request and Promise in the WeChat applet? The following is a practical case. Let’s take a look.
When using Promise, my multiple asynchronous codes usually look like this.ajax(url, function (res){ ajax(res.url, function(res) { ajax(res.url, function(res) { if (res.status == '1') { ajax(res.url, function(res) { ... } } else if (res.status == '2') { ajax(url2, function(res) { ... } ... } } } );This kind of process is very labor-intensive and fragile, and the experience is very bad. Therefore, in this small program In order to have a better experience in the development, I started to use Promise.The code is as follows. In this way, when our second request requires the first parameter judgment, we can no longer fall into callback hell,
// 小程序与后端情求接口 let baseUrlPromise = 'https://xxx.com'; // 定义方法返回Promise参数,obj 为wx.request 方法中所需参数 let req = function (obj) { return new Promise(function (resolve, reject) { wx.request({ url: baseUrlPromise + obj.url, data: obj.data, header: obj.header, method: obj.method == undefined ? "get" : obj.method, success: function (data) { // 回调成功执行resolve resolve(data) }, fail: function (data) { // 回调失败时 if (typeof reject == 'function') { reject(data); } else { console.log(data); } }, }) }) } // 执行req 方法,传入第一个请求, let req1 = req({ url: '第一次请求链接,与baseUrlPromise 相结合', data: {}, }) // 当需要多次请求时加入 req1.then(function (data) { console.log('promiseThen1') console.log(data); return req({ url: '第二次请求链接', }) }).then(function (data) { console.log('promiseThen3') console.log(data); return req({ url:'第三次请求链接' }) }).then(......).catch(function(data){ console.log(PromiseCatch) })I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
The above is the detailed content of Combination use of wx.request and Promise of WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
