【问题标题】:Is there an example of using fetch with flowjs?有没有使用 fetch 和 flowjs 的例子?
【发布时间】:2017-03-08 21:56:26
【问题描述】:

我正在尝试在我的异步函数中使用 fetch,但流程抛出此错误

错误:(51, 26) 流程:承诺。此类型与联合不兼容:标识符Promise 的类型应用 | await 的类型参数T

这是一个可以产生这个错误的代码:

async myfunc() {
   const response = await fetch('example.com');
   return await response.json();
}

我想输入response.json的回复

【问题讨论】:

标签: javascript types fetch flowtype


【解决方案1】:

您可以使用Promise <T> 注释函数的返回类型,其中T 是所需的类型,或者使用显式类型注释将结果分配给临时本地,然后返回该本地。然后将推断函数返回类型。

显式返回类型注释:

async myfunc(): Promise<{name: string}> {
    const response = await fetch('example.com');
    return await response.json();
}

从显式注释的本地推断返回类型:

async myfunc() {
    const response = await fetch('example.com');
    const result: {name: string} = await response.json();
    return result;
}

【讨论】:

    猜你喜欢
    • 2018-11-01
    • 1970-01-01
    • 2020-11-13
    • 2012-02-22
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    相关资源
    最近更新 更多