【发布时间】:2020-06-06 07:06:30
【问题描述】:
我有一个应按顺序执行的作业列表。 由于作业需要几秒钟才能完成,因此应该在后台运行。 我认为一份工作可以描述为
interface Job {
name: string
execute(): Promise<boolean>
}
我想要一个函数来获取这个作业列表并按顺序执行它们 直到列表完成或一项工作失败或被拒绝,所以基本上:
function executeUntilFailed(jobs: Job[]): Promise<boolean>
{
// execute first job
// if this job
// - returns with true: continue with the next job
// - returns with false: resolve the promise with false
// - got rejected: reject the promise with the reason prefixed with the jobs name
//
// if there are no more jobs to do, resolve the promise with true
//
// basically it's a reduce operation with starting value of true and
// early stops if one job yields false or got rejected
}
我对 Javascript/Typescript 比较陌生,很难实现。
谢谢, 迪特
【问题讨论】:
-
async function executeUntilFailed(jobs: Job[]) { for(const job of jobs) {if (!await job.execute()) {return false};} }。没什么
标签: javascript typescript promise