Javascript 轮询API,直到响应对象中的路径成功|失败-类型脚本
我有一个函数,它接受字符串和路径值,并检查结果处的路径是否返回1或-1。我使用多个请求启动该函数,除一个请求外,所有请求似乎都成功。例如,如果我连续调用10个不同URL的函数(一个接一个,不是在一个数组中),承诺将为9解析,但不会为第10个解析 这是我的代码:Javascript 轮询API,直到响应对象中的路径成功|失败-类型脚本,javascript,typescript,promise,async-await,settimeout,Javascript,Typescript,Promise,Async Await,Settimeout,我有一个函数,它接受字符串和路径值,并检查结果处的路径是否返回1或-1。我使用多个请求启动该函数,除一个请求外,所有请求似乎都成功。例如,如果我连续调用10个不同URL的函数(一个接一个,不是在一个数组中),承诺将为9解析,但不会为第10个解析 这是我的代码: enum Status { Queued = 0, Started = 1, Finished = 2, Failed = -1, } let dataFetchingTimer: number; export con
enum Status {
Queued = 0,
Started = 1,
Finished = 2,
Failed = -1,
}
let dataFetchingTimer: number;
export const getDataAtIntervals = (url: string, path: string): Promise<any> => {
clearTimeout(dataFetchingTimer);
return new Promise<any>((resolve: Function, reject: Function): void => {
try {
(async function loop() {
const result = await API.load(url);
console.log(`${url} - ${JSON.stringify(result)}`)
if (
get(result, path) &&
(get(result, path) === Status.Finished ||
get(result, path) === Status.Failed)
) {
return resolve(result); // Resolve with the data
}
dataFetchingTimer = window.setTimeout(loop, 2500);
})();
} catch (e) {
reject(e);
}
});
};
export const clearGetDataAtIntervals = () => clearTimeout(dataFetchingTimer);
enum状态{
排队=0,
开始=1,
完成=2,
失败=-1,
}
让dataFetchingTimer:编号;
export const getDataAtIntervals=(url:string,path:string):承诺
请给我一些建议。在上图中,4535只被调用一次。并且在返回2或-1之前不会被调用。对所有调用使用单个超时可能是导致奇怪行为的原因。避免调用之间发生冲突的解决方案可能是每次调用都使用超时。您可以按照这些思路做一些事情(我使用简单的JS,因为我不习惯打字):
如果不能运行它,说起来并不容易,但我的猜测是,它与共享相同dataFetchingTimer
变量的所有请求有关。不清楚您如何调用它,或者您的输出对应什么,我将其称为const result=wait getDataAtIntervals(url,“status”)。Setstate(oldState=>{return{data:{…oldState.data,result}}})我可以创建多个settimeout吗?我想可以。我不习惯打字,所以我用的是JS,但这可能会让你想到一些可能的事情:@blex谢谢。我会尝试让你知道
const Status = {
Queued: 0,
Started: 1,
Finished: 2,
Failed: -1,
}
let dataFetchingTimerMap = {
// Will contain data like this:
// "uploads/4541_status": 36,
};
const setDataFetchingTimer = (key, cb, delay) => {
// Save the timeout with a key
dataFetchingTimerMap[key] = window.setTimeout(() => {
clearDataFetchingTimer(key); // Delete key when it executes
cb(); // Execute the callback
}, delay);
}
const clearDataFetchingTimer = (key) => {
// Clear the timeout
clearTimeout(dataFetchingTimerMap[key]);
// Delete the key
delete dataFetchingTimerMap[key];
}
const getDataAtIntervals = (url, path) => {
// Create a key for the timeout
const timerKey = `${url}_${path}`;
// Clear it making sure you're only clearing this one (by key)
clearDataFetchingTimer(timerKey);
return new Promise((resolve, reject) => {
// A try/catch is useless here (https://jsfiddle.net/4wpezauc/)
(async function loop() {
// It should be here (https://jsfiddle.net/4wpezauc/2/)
try {
const result = await API.load(url);
console.log(`${url} - ${JSON.stringify(result)}`);
if ([Status.Finished, Status.Failed].includes(get(result, path))) {
return resolve(result); // Resolve with the data
}
// Create your timeout and save it with its key
setDataFetchingTimer(timerKey, loop, 2500);
} catch (e) {
reject(e);
}
})();
});
};
const clearGetDataAtIntervals = () => {
// Clear every timeout
Object.keys(dataFetchingTimerMap).forEach(clearDataFetchingTimer);
};