wx.request 同步阻塞问题?
发布于 6 年前 作者 dcui 12551 次浏览 来自 官方Issues

我小程序有个需求,wx.request返回值要放到一个集合中,这个封装必须卸载for循环内,意思我要请求网页多次;我也封装了,我什么取得值还是空。

utils/time

  for (var i = 0; i < 10; i++) {
    var weekDay = {
      week: ‘’,
      day: ‘’,
      color:’’,
      yearMonth:’’
    }
    var time = toDaytime + i * 24 * 60 * 60 * 1000
    weekDay.week = getWeek(time)
    weekDay.day = getMyDay(time)
    weekDay.yearMonth = getYearMonth(time)
   
    demo()
    async function demo(time) {
    var time1 = new Date(time);
    var date = time1.getFullYear() +  appendZero(time1.getMonth() + 1) + appendZero(time1.getDate());
    const postData = require("…/utils/request.js");//引用
    await postData('http://api.goseek.cn/Tools/holiday’, “2”).then((res) => {
        console.log(“333” + rec)
      })
    }
    timeBean.weekDayList.push(weekDay)
  }
 
 util/request
 
 import regeneratorRuntime from ‘…/…/utils/runtime.js’;

const postData = async function (url, data) {
  wx.showLoading({
    title: ‘加载中’,
  })
  let promiseP = await new Promise(function (resolve, reject) {
    wx.request({
      url: baseUrl + url,
      data: data,
      method: ‘POST’,
      header: {
        ‘content-type’: ‘application/json’,
        ‘access-token’: wx.getStorageSync(‘token’)
      },
      success: function (res) {
        wx.hideLoading();
        if (res.statusCode === 200) {
          resolve(res)
        } else {
          reject(res.data)
        }
      },
      fail: function (err) {
        wx.hideLoading();
        reject(err)
        if (err.code === 401) { }
      }
    })
  })
  return promiseP
}
module.exports = {
  postData
}

回到顶部