import { SeriesNextCallback, SeriesFunction } from "../interfaces"; import { once } from "../common"; /** * This method executes a series of functions one by one in order, or break * if an error occured. Finally, the final callback will be call, with the * ending reason if exists. */ export function series( fns: SeriesFunction[], final?: SeriesNextCallback ): void { fns = fns.slice(); let loop = function(i: number, ...args: any[]): void { if (i < fns.length) { fns[i](once(function(err?: E, ...args: any[]): void { if (err !== null && err !== undefined) { final && final(err); } else { loop(i + 1, ...args); } }), ...args); } else { final && final(); } }; loop(0); }