类型“ void”必须具有返回迭代器的“ [Symbol.iterator]()”方法

时间:2019-05-20 16:40:44

标签: javascript typescript

运行下面的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])
  }

如何使此代码正确运行?

1 个答案:

答案 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();