Home >WeChat Applet >WeChat Development >Combination use of wx.request and Promise of WeChat applet

Combination use of wx.request and Promise of WeChat applet

php中世界最好的语言
php中世界最好的语言Original
2018-03-19 16:28:283642browse

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:

Using Vue instructions

Using JS closures

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!

Statement:
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