UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

53 lines (50 loc) 1.9 kB
export let ResultStatus = /*#__PURE__*/function (ResultStatus) { ResultStatus["FULFILLED"] = "fulfilled"; ResultStatus["FAILED"] = "failed"; return ResultStatus; }({}); const isFullfilled = result => result.status === ResultStatus.FULFILLED; const markFullfilled = value => ({ status: ResultStatus.FULFILLED, value: value }); const markRejected = error => ({ status: ResultStatus.FAILED, reason: error }); /** * Will wait for all promises to resolve or reject, wrapping their real results in * object containing the status so it's easy to filter it later. Loosely inspired by * [Promise.allSettled](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) * which can replace this implementation once it makes to the browsers. * @param promises */ export const waitForAllPromises = promises => { return Promise.all(promises.map(result => result.then(markFullfilled).catch(markRejected))); }; /** * Will resolve on the first fulfilled promise and disregard the remaining ones. Similar to `Promise.race` but won't * care about rejected promises. * @param promises */ export const waitForFirstFulfilledPromise = promises => { const rejectReasons = []; return new Promise((resolve, reject) => { promises.forEach(promise => promise.then(value => { if (typeof value === 'undefined' || value === null) { throw new Error(`Result was not found but the method didn't reject/throw. Please ensure that it doesn't return null or undefined.`); } resolve(value); }).catch(reason => { rejectReasons.push(reason); if (rejectReasons.length === promises.length) { reject(reason); } })); }); }; /** * Find all fullfilled promises and return their values * @param results */ export const getOnlyFulfilled = results => results.filter(isFullfilled).map(result => result.value);