Skip to content

Commit

Permalink
Implement Promise.allSettled() (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
retyui committed Aug 31, 2022
1 parent 14a9a38 commit d435c5e
Show file tree
Hide file tree
Showing 4 changed files with 385 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ Promise.all([Promise.resolve('a'), 'b', Promise.resolve('c')])
})
```

#### Promise.allSettled(array)

Returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.

```js
Promise.allSettled([Promise.resolve('a'), Promise.reject('error'), Promise.resolve('c')])
.then(function (res) {
res[0] // { status: "fulfilled", value: 'a' }
res[1] // { status: "rejected", reason: 'error' }
res[2] // { status: "fulfilled", value: 'c' }
})
```

#### Promise.race(array)

Returns a promise that resolves or rejects with the result of the first promise to resolve/reject, e.g.
Expand Down
94 changes: 93 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ interface ThenPromise<T> extends Promise<T> {
nodeify(callback: (err: Error, value: T) => void): void;
}

interface PromiseFulfilledResult<T> {
status: "fulfilled";
value: T;
}

interface PromiseRejectedResult {
status: "rejected";
reason: any;
}

type PromiseSettledResult<T> = PromiseFulfilledResult<T> | PromiseRejectedResult;

interface ThenPromiseConstructor {
/**
* A reference to the prototype.
Expand All @@ -48,6 +60,86 @@ interface ThenPromiseConstructor {
*/
new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => any): ThenPromise<T>;

/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>, PromiseSettledResult<T6>, PromiseSettledResult<T7>, PromiseSettledResult<T8>, PromiseSettledResult<T9>, PromiseSettledResult<T10>]>;

/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>, PromiseSettledResult<T6>, PromiseSettledResult<T7>, PromiseSettledResult<T8>, PromiseSettledResult<T9>]>;

/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>, PromiseSettledResult<T6>, PromiseSettledResult<T7>, PromiseSettledResult<T8>]>;

/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>, PromiseSettledResult<T6>, PromiseSettledResult<T7>]>;

/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>, PromiseSettledResult<T6>]>;

/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>]>;

/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>]>;

/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>]>;

/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>]>;

/**
* Creates a Promise that is resolved with an array of results when all
* of the provided Promises resolve or reject.
* @param values An array of Promises.
* @returns A new Promise.
*/
allSettled<T>(values: (T | PromiseLike<T>)[]): ThenPromise<PromiseSettledResult<T>[]>;

/**
* Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any ThenPromise is rejected.
Expand Down Expand Up @@ -243,4 +335,4 @@ interface ThenPromiseConstructor {

declare var ThenPromise: ThenPromiseConstructor;

export = ThenPromise;
export = ThenPromise;
23 changes: 23 additions & 0 deletions src/es6-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ Promise.all = function (arr) {
});
};

function onSettledFulfill(value) {
return { status: 'fulfilled', value: value };
}
function onSettledReject(reason) {
return { status: 'rejected', reason: reason };
}
function mapAllSettled(item) {
if(item && (typeof item === 'object' || typeof item === 'function')){
if(item instanceof Promise && item.then === Promise.prototype.then){
return item.then(onSettledFulfill, onSettledReject);
}
var then = item.then;
if (typeof then === 'function') {
return new Promise(then.bind(item)).then(onSettledFulfill, onSettledReject)
}
}

return onSettledFulfill(item);
}
Promise.allSettled = function (iterable) {
return Promise.all(iterableToArray(iterable).map(mapAllSettled));
};

Promise.reject = function (value) {
return new Promise(function (resolve, reject) {
reject(value);
Expand Down
Loading

0 comments on commit d435c5e

Please sign in to comment.