有使用 fetch 和 flowjs 的例子吗?

Is there an example of using fetch with flowjs?

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

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

的类型参数 T

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

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

我想输入 response.json

的回复

您可以使用 Promise <T> 注释函数的 return 类型,其中 T 是所需的类型,或者使用显式类型注释将结果分配给临时局部变量,然后return那个地方。然后将推断函数 return 类型。

显式 return 类型注释:

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

从显式注释的本地推断出 return 类型:

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