【发布时间】:2020-03-18 09:10:17
【问题描述】:
我有一个 generator 函数 foo(),我在其中使用 fetch 调用 API。
从 API 收到 response 后,我将其解析为 JSON。
Typescript 抛出错误:Object of type 'unknown' in this line -> const msg = yield response.json();
function* foo(val: ValType): Generator {
const response = yield fetch(endPoint, {
method: 'POST',
body: JSON.stringify(val),
});
if (response) {
// typescript throws error
// that type is unknown for
// the response object
const msg = yield response.json();
return msg;
}
}
【问题讨论】:
-
foo的消费者可以yield为所欲为。你的意思是用await代替yield,用async代替*? -
@CertainPerformance 所说的:typescriptlang.org/play/?target=99#code/…
-
您应该使用 async 和 await 只是为了确保在测试之前获取数据是否有任何结果
-
@CertainPerformance 我并不是要使用
await,因为我在Redux Saga 中使用foo。我不确定async/await是否可以在 Redux Saga 中使用,因为只能使用生成器函数。 -
@besartm 我无法使用
async/await,因为我正在使用 Redux Saga
标签: reactjs typescript fetch generator redux-saga