运行下面的post()
函数时,出现以下错误信息:
Type 'void' must have a '[Symbol.iterator]()' method that returns an iterator.
这是我要运行的内容:
static async post(options:object) {
const promise = postRequest(options)
const [err, data] = await this.promiseWapper(promise);
return [err, data]
}
static async promiseWapper(promise: any) {
return
promise()
.then((res: any) => [undefined, res])
.catch((err: any) => [err, undefined])
}
如何使此代码正确运行?
答案 0 :(得分:2)
我修复了您的代码。 PromiseWrapper将诺言称为功能promise()
,但这不是函数,而是诺言。假设postRequest
返回了一个承诺,即...
async function post(options) {
const promise = postRequest(options)
const [err, data] = await this.promiseWapper(promise);
return [err, data]
}
async function promiseWapper(promise) {
return promise
.then((res) => [undefined, res])
.catch((err) => [err, undefined])
}
function postRequest() {
return Promise.resolve(true);
}
post();