// move some of the promise related methods here import type { AnyType, ProcessAllResult } from './types' import { merge } from './lodash' /* looks silly but save a lot of typing in ava async test */ export const promise = async (cb: AnyType) => new Promise(cb) /** This is basically the process chain promises the different is even when some failed we will not throw it and exit, instead we put that in the fail result array */ async function _processAll( promises: Array> ): Promise { return promises.reduce((promiseChain, currentTask) => ( promiseChain.then(chainResults => ( currentTask .then(currentResult => ( merge(chainResults, { done: [...chainResults.done, currentResult] }) )) .catch(err => ( merge(chainResults, { fail: [...chainResults.fail, err] }) )) )) ), Promise.resolve( { done: [], fail: [] } )) } /** we unwrap the result to make it more generic */ export async function processAll( promises: Array> ): Promise> { // annoying TS add a Promise crap in downstream return _processAll(promises) .then((result: ProcessAllResult) => { const { done, fail } = result const res: Array = [] // 1.3.1 always return the fail for easier checking res.push(done, fail) return res }) }