[英]timer wrapper function for recursive async/await call
我有一个递归异步函数getResponse(url,attempts = 0)
,该函数轮询外部api的响应并在达到X次重试次数或服务器错误后解析或退出。 但是,它的内部“时钟”基于重试的次数(允许延迟以避免速率限制后),但是我还希望在设置基于时间的计时器方面具有灵活性,这将解决该功能并结束递归。 理想情况下,我希望能够将基于时间的计时器包装在我的递归异步函数周围,例如timed(getResponse(url),3400)
我仅通过将两个计时器打包到一个异步函数中并将本地变量expired
作为退出标志,并在两个函数上设置Promise.race条件,使基于时间的计时器和基于“重试”的计时器一起工作。
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)
解决方案的任何帮助。
我的功能可能会满足您的需求。 我已经根据我对您的需求的理解对其进行了更新。 想法是您将轮询直到某些事情成为事实,即解决或您超过最大尝试次数。 它具有内置的可配置延迟。
这里的想法是您传递一个包装您的fetch调用的函数,该函数最终将解析/拒绝。
setPolling(pollFunc,freq = 1000,maxAttempts = 3)
pollFunc =不带参数的函数,返回一个最终解决或拒绝的promise。
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}`))
基于要在计时器到期时停止重试的基础,则可以使用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));
请注意,在设置停止信号时:
再多考虑一下,可以根据实际需要更改这些行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.