将小游戏异步 API 转换为返回 AsyncResult<T, E> 的新函数,需要转换的 API 必须是接受可选 success 和 fail 回调的函数,并且其返回值必须是 void 或 Promise。
AsyncResult<T, E>
success
fail
void
Promise
其中 T 为 success 回调的参数类型,E 为 fail 回调的参数类型。
T
E
小游戏异步 API。
返回一个新的函数,该函数返回 AsyncResult<T, E>。
// 将 wx.setStorage 转换为 Promise 风格const setStorageAsync = promisifyWithResult(wx.setStorage);const result = await setStorageAsync({ key: 'test', data: 'value' });if (result.isOk()) { console.log('存储成功');} else { console.error('存储失败:', result.unwrapErr());} Copy
// 将 wx.setStorage 转换为 Promise 风格const setStorageAsync = promisifyWithResult(wx.setStorage);const result = await setStorageAsync({ key: 'test', data: 'value' });if (result.isOk()) { console.log('存储成功');} else { console.error('存储失败:', result.unwrapErr());}
将小游戏异步 API 转换为返回
AsyncResult<T, E>的新函数,需要转换的 API 必须是接受可选success和fail回调的函数,并且其返回值必须是void或Promise。其中
T为success回调的参数类型,E为fail回调的参数类型。