微信小程序等待request的返回值

在我的event.js文件中,用到了一个if()判断:

if(isCoincide(参数...)){
} else{
}

我的isCoincide定义在function.js中:

export function isCoincide(start_date, end_date, token){
  var res_data
  var promise = new Promise((resolve, reject) => {
    wx.request({
      url: url,
      data: {},
      header: header,
      success: function (res) {
        resolve(res);
        res_data = res.data
      },
      fail: function (res) {
        reject(res.errMsg || 'failed');
      }
    });
  });
  promise.then(()=>{
    console.log(res_data)
    return judgeCoincide(start_date, end_date, res_data)
  })
  console.log("end")
}

然后我的judgeCoincide用来判断wx.request请求获得的数据是否满足条件,满足则返回true

我想要在event.js获得isCoincide的返回值,但是因为微信小程序的request是异步请求,每次不等待request的结果直接就返回了,导致我的if()判断一直是true,有什么办法可以解决吗?

我在网上找了很久,promise貌似并不能解决异步的问题,而且我不想在代码最后加while(true)判断或者setTimeOut这种东西,怎样可以强制让isCoincide等待wx.request的返回值并判断返回值是否满足我的要求呢?

阅读 3.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题