export interface IResolve { (value?: T | PromiseLike): void; } export interface IReject { (reason?: any): void; } export type TDeferredStatus = 'pending' | 'fulfilled' | 'rejected'; export class Deferred { public promise: Promise; public resolve: IResolve; public reject: IReject; public status: TDeferredStatus; public startedAt: number; public stoppedAt: number; public get duration(): number { if (this.stoppedAt) { return this.stoppedAt - this.startedAt; } else { return Date.now() - this.startedAt; } } constructor() { this.promise = new Promise((resolve, reject) => { this.resolve = (valueArg: T | PromiseLike) => { this.status = 'fulfilled'; this.stoppedAt = Date.now(); resolve(valueArg); }; this.reject = (reason: any) => { this.status = 'rejected'; this.stoppedAt = Date.now(); reject(reason); }; this.startedAt = Date.now(); this.status = 'pending'; }); } } export let defer = () => { return new Deferred(); }; /** * Creates a new resolved promise for the provided value. */ export let resolvedPromise = (value?: T): Promise => { return Promise.resolve(value); }; /** * Creates a new rejected promise for the provided reason. */ export let rejectedPromise = err => { return Promise.reject(err); }; interface IAsyncFunction { (someArg: T): Promise; } export let map = async (inputArg: T[], functionArg: IAsyncFunction) => { let promiseArray: Promise[] = []; let resultArray = []; for (let item of inputArg) { let promise: Promise = functionArg(item); promiseArray.push(promise); promise.then(x => { resultArray.push(x); }); } await Promise.all(promiseArray); return resultArray; };