我有一个递归异步函数getResponse(url,attempts = 0)
,该函数轮询外部api的响应,并在达到X次重试次数或服务器错误后解析或退出。
但是,它的内部“时钟”基于重试的次数(允许延迟以避免速率限制后),但是我还希望在设置基于时间的计时器方面具有灵活性,这将解决该功能并结束递归。理想情况下,我希望能够将基于时间的计时器包装在我的递归异步函数周围,例如timed(getResponse(url),3400)
通过将两个计时器打包到一个异步函数中,并将本地变量expired
用作退出标志,并将Promise.race条件设置为on,我只能设法同时使用基于时间和基于“重试”的计时器这两个功能。
async function timedgetResponse (expiry = 3500,url) {
let expired = false;
async function timeout(expiry){
await new Promise(_=> setTimeout(_,expiry));
expired = true;
return false;
};
async function getResponse(url,attempts = 0){
try {
if(expired){ return false; };
const limit = 10;
if(attempts >= limit){ok: false, e:"MAX_ATTEMPTS"};
const rawRes = await fetch(url,
{
method: 'GET',
credentials: 'include',
headers: {
'Accept': 'application/json'
}
});
if (!rawRes.ok) { throw (Error('SERVER_ERROR')); };
const res = await rawRes.json();
if(!res || res.status === 0){ throw (Error(res.request)); };
return {ok: true, res: res.request};
} catch(e){
const err = e.message;
if(err === "RESPONSE_NOT_READY"){
await new Promise(_ => setTimeout(_, 333));
attempts +=1;
return getResponse(url,attempts);
} else
if(err === "SERVER_ERROR_ON_RESOLVER"){
await new Promise(_ => setTimeout(_, 10000));
attempts +=1;
return getResponse(url,attempts);
} else {
return {ok: false, e:"MISC_ERROR"};
};
};
};
const awaited = await Promise.race([
getResponse(url),
timeout(expiry)
]);
return awaited;
};
我认为这不是正确的方法,希望能为timed(getResponse(url),3400)
解决方案提供帮助。
答案 0 :(得分:0)
我有一个可能满足您需要的功能。我已经根据我对您的需求的理解对其进行了更新。想法是您将轮询直到某些事情成为事实,即解决或您超过最大尝试次数。它具有内置的可配置延迟。
这里的想法是您传递一个包装您的fetch调用的函数,该函数最终将解决/拒绝。
setPolling(pollFunc,freq = 1000,maxAttempts = 3)
pollFunc =不带参数的函数,并返回最终解决或拒绝的承诺。
freq =以毫秒为单位运行pollFunc的频率
maxAttempts =放弃前的最大尝试次数
const setPolling = async (pollFunc, freq = 1000, maxAttempts = 3, _attempts = 1) => {
const wait = (delay) => new Promise(resolve=>setTimeout(resolve, delay))
try {
return await pollFunc()
} catch (e) {
if (_attempts < maxAttempts) {
await wait(freq)
return await setPolling(pollFunc, freq, maxAttempts, ++_attempts)
}
throw (e instanceof Error) ? e : new Error((typeof e !== 'undefined') ? e : 'setPolling maxAttempts exceeded!')
}
}
async function alwaysFail() {
throw new Error(`alwaysFail, failed because that's what it does!`)
}
function passAfter(x) {
let i = 0
return async ()=> {
if (i > x) return `passAfter succeeded because i(${i}) > x(${x})`
throw new Error(`i(${i++}) < x(${x})`)
}
}
setPolling(alwaysFail)
.catch((e)=>console.error(`alwaysFail, failed!\n${e.message}\n${e.stack}`))
setPolling(passAfter(5), 500, 10)
.then((res)=>console.log(`passAfter, succeeded!\n${res}`))
.catch((e)=>console.error(`passAfter, failed!\n${e.message}\n${e.stack}`))
答案 1 :(得分:0)
根据您想在计时器到期时停止重试的情况,可以使用token
将停止信号传递给递归过程。
应该执行以下操作:
const poll = async (work, options, token) => {
const settings = Object.assign({ 'delay':0, 'attempts':1, 'maxAttempts':3 }, options);
// Utility function which returns a Promise-wrapped setTimeout
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// Two mechanisms for stopping the recursion.
// Performing the tests here ensures they are applied before the first work() call.
// 1. token-borne stop signal
if(token.stop) {
throw new Error('poll(): stopped');
}
// 2. max attempts reached
if (settings.attempts >= settings.maxAttempts) {
throw new Error('poll(): max attempts reached');
}
// Do the work, and recurse on error
try {
return await work();
}
catch (e) {
await delay(settings.delay);
settings.attempts += 1; // Mutate/pass `settings`; the original `options` is not guaranteed to have an `attempts` property.
return await poll(work, settings, token);
}
}
致电如下:
// token
const token = {}; // or {'stop':false} if you like
// Time based timer:
setTimeout(() => {
token.stop = true; // raise the 'stop' flag
}, 60000); // or whatever
let pollPromise = poll(doSomethingAsync, {'delay':1000, 'maxAttempts':100}, token)
.then((res) => console.log(res))
.catch((e) => console.error(e));
请注意,在设置停止信号时:
再想一想,这些行为可能会根据所需的具体情况而改变。