【发布时间】:2021-07-06 21:21:09
【问题描述】:
我有一个获取外部 API 凭据的函数(过于简单):
const fetchCredentials= async () => {
return await fetch(/* url and params */);
};
另一个调用上述方法并在响应不正常时继续重试调用。
const retryFetchCredentials = (initialDelay = 250): Promise<Credentials | void> => {
return fetchCredentials().then(async res => {
if (res.ok) {
const parsedResponse = await res.json() as Credentials ;
return parsedResponse;
}
else {
// The issue is with this timeout/return:
setTimeout(() => {
return retryFetchCredentials (initialDelay * 2);
}, initialDelay);
}
});
};
我的问题是我不知道如何在 setTimeOut 函数中强输入 return,我不断收到 Promise returned in function argument where a void return was expected. 错误。我尝试了函数retryFetchCredentials 的几种返回类型,但均无济于事。
关于如何解决这个问题的任何线索?
【问题讨论】:
-
从
setTimeout的回调返回与从.then回调返回不相同。您可能需要将超时包装在new Promise中,以便您可以resolve值。 -
好的,这个,连同下面拉斯的回答,成功了,谢谢!!
标签: node.js typescript asynchronous node-fetch