当使用 3rd 方库(例如 Cloud firestore)提供的异步函数或 observables 时,我发现函数waitFor
如下所示的方法(TypeScript,但你明白了......)当你需要等待某个进程时很有帮助完成,但您不想在回调内的回调中嵌入回调,也不想冒无限循环的风险。
此方法有点类似于while (!condition)
睡眠循环,但异步产生并定期对完成条件执行测试,直到为真或超时。
export const sleep = (ms: number) => {
return new Promise(resolve => setTimeout(resolve, ms))
}
/**
* Wait until the condition tested in a function returns true, or until
* a timeout is exceeded.
* @param interval The frenequency with which the boolean function contained in condition is called.
* @param timeout The maximum time to allow for booleanFunction to return true
* @param booleanFunction: A completion function to evaluate after each interval. waitFor will return true as soon as the completion function returns true.
*/
export const waitFor = async function (interval: number, timeout: number,
booleanFunction: Function): Promise<boolean> {
let elapsed = 1;
if (booleanFunction()) return true;
while (elapsed < timeout) {
elapsed += interval;
await sleep(interval);
if (booleanFunction()) {
return true;
}
}
return false;
}
假设您在后端有一个长时间运行的进程,您想在执行其他任务之前完成。例如,如果您有一个汇总帐户列表的函数,但您想在计算之前从后端刷新帐户,您可以执行以下操作:
async recalcAccountTotals() : number {
this.accountService.refresh(); //start the async process.
if (this.accounts.dirty) {
let updateResult = await waitFor(100,2000,()=> {return !(this.accounts.dirty)})
}
if(!updateResult) {
console.error("Account refresh timed out, recalc aborted");
return NaN;
}
return ... //calculate the account total.
}