IOS端 当Promise与异步函数一起使用时 特定情况下永远不会settled

小程序 文章 2022-01-06 12:00 438 0 全屏看文

AI助手支持GPT4.0

IOS端 当Promise与异步函数一起使用时 特定情况下永远不会settledIOS side When Promise is used with asynchronous function, it will never be settled under certain circumstances

IOS端 当Promise与异步函数一起使用时 特定情况下永远不会settled
框架类型 问题类型 终端类型 微信版本 基础库版本
小程序 Bug 微信iOS客户端 8.0.16 2.22.3
const app = getApp()function wait(delay{  return new Promise(resolve => setTimeout(resolve, delay))}const requestAuth = async() =>  {  return request(false)}async function request(auth = true{    await Promise.resolve()    await wait(300);    if (auth) {      await requestAuth()    }    return true}Page({  onLoad() {        request().then(          () => {            //在ios既不会执行到这里 也不会报错            wx.showModal({              title'在ios上始终无法出现的modal',            })          },          console.error        )  },})

Framework type problem type terminal type wechat version basic library version applet bug wechat IOS client 8.0.16 2.22.3 const app = getapp() function wait (delay) {return new promise (resolve = > setTimeout (resolve, delay))} const} requestauth = async() = > {return} async} function request (auth = true) {await {promise. Resolve() await} wait (300); if (auth) {await {requestauth()} return} true} page ({onload() {request(). Then (() = > {/ / this will not be executed here in IOS. No error will be reported. Wx.showmodal( {Title: 'module that cannot appear on IOS',})}, console Error)},}) code snippet https://developers.weixin.qq.com/s/L6obm6mt70w3

回答:

老张:

1、碰到过。一律放弃promise方式,改e5异步的写法。

Reynold陈靖:

你return一个true,true身上有.then方法吗?

Jesse:这跟我return什么没关系吧
Jesse:难道异步函数中不允许return true吗
Reynold陈靖:不是不允许return true。我认为链式调用,后面的.then()方法依赖的是上一个函数的返回值
Jesse:兄弟,你应该好好看看异步函数的用法,异步函数被调用后始终返回Promise
xplee:async/await 以及 Promise,回炉重炼一下吧
杨泉:true.then?
Jesse:兄弟,你是在说我嘛
Jesse:哪写错了,请指教
Reynold陈靖:你是对的,我写了个demo验证了。那会不会是小程序环境不同,不支持这种写法?
Jesse:大哥,注意我这是异步函数
Jesse:很有可能微信没有按照EcmaScript实现Promise
xplee:云函数才敢用async await,小程序端,我一般用直接返回Promise对象的方式,调用端采用链式调用。
Jesse:异步函数兼容性了解下 ios >= 10.3 & android >= 55
Jesse:

我真服了,昨天因为找这个bug 我10点才下班

-EOF-

AI助手支持GPT4.0