678
679
export function sequence<T>(promiseFactories: ITask<Promise<T>>[]): Promise<T[]> {
681
>
let index = 0;
682
>
const len = promiseFactories.length;
683
>
684
>
function next(): Promise<T> | null {
685
>
return index < len ? promiseFactories[index++]() : null;
686
>
}
687
>
688
>
function thenHandler(result: unknown): Promise<any> {
689
>
if (result !== undefined && result !== null) {
690
>
results.push(result as T);
691
>
}
692
>
693
>
const n = next();
694
>
if (n) {
695
>
return n.then(thenHandler);
696
>
}
697
>
698
>
return Promise.resolve(results);
699
>
}
700
>
701
>
return Promise.resolve(null).then(thenHandler);
702
>
}
703
704
export function first<T>(promiseFactories: ITask<Promise<T>>[], shouldStop: (t: T) => boolean = t => !!t, defaultValue: T | null = null): Promise<T | null> {