比如说这个方法 wx.getFileSystemManager().readFile/readFileSync
这个方法是将图片转成base64的 这个需要发送网络请求
然后会耗时 有时候图片还没转成base64 我的方法就已经全部执行完了
这个有什么解决办法呢
我目前的方案是把方法其余的代码 都写在 wx.getFileSystemManager().readFile/readFileSync 的回调里面
不单单只有这个方法 很多方法都这样
也不可能用 setTimeout 去延迟进行操作, 并且如果网速慢 wx方法的回调也就返回的更慢
大家有没有比较好的办法

了解一下Promise
https://developers.weixin.qq.com/miniprogram/dev/extended/utils/api-promise.html
不能直接引入js把 我看到那个文档 要npm安装
Page({onLoad: async(){var res = await this.promiseFunc()// 等待API执行结束后,才console出结果console.log(res)},promiseFunc(){return new Promise((rs, rj)=>{// setTimeout只是模拟需要执行耗时的Api请求setTimeout(()=>{// res 模拟API返回值var res = {a:1, b:2}rs(res)}, 5000)})}})promise: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise
async/await https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/async_function
还是觉得这个方法不够好的话,出门右转google js如何处理异步事件,这个问题实在与小程序无关。
promise
异步处理数据问题。用Promise处理。
readFileSync 这个不是同步方法吗?需要写在回调里?