Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type signature for Promise.allSettled() #8230

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,12 +688,16 @@ declare class Promise<+R> {
static resolve<T>(object: Promise<T> | T): Promise<T>;
static reject<T>(error: any): Promise<T>;
static all<T: Iterable<mixed>>(promises: T): Promise<$TupleMap<T, typeof $await>>;
static allSettled<T: Iterable<mixed>>(promises: T): Promise<$TupleMap<T, typeof $settlement>>;
static race<T, Elem: Promise<T> | T>(promises: Iterable<Elem>): Promise<T>;
}

// we use this signature when typing await expressions
declare function $await<T>(p: Promise<T> | T): T;

declare function $settlement<T>(p: Promise<T> | T): SettlementResult<T>;
declare type SettlementResult<+T> = {| status: 'fulfilled', +value: T |} | {| status: 'rejected', reason: mixed |};

/* Binary data */

declare class ArrayBuffer {
Expand Down
83 changes: 83 additions & 0 deletions tests/core_tests/allSettled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// @flow

function* generator(): Iterable<Promise<number>> {
while (true) {
yield Promise.resolve(1);
}
}

let tests = [
// good cases
function() {
let p1 : Promise<[]> = Promise.allSettled([]);
let p2 : Promise<[
SettlementResult<number>,
SettlementResult<string>,
SettlementResult<boolean>,
]> = Promise.allSettled([1, '', false]);
let p3 : Promise<[
SettlementResult<number>,
SettlementResult<string>,
SettlementResult<boolean>,
]> = Promise.allSettled([
Promise.resolve(1),
Promise.resolve(''),
Promise.resolve(false),
]);
let p4 : Promise<[
SettlementResult<number>,
SettlementResult<number>,
]> = Promise.allSettled([
1,
Promise.resolve(1),
]);
let p5 : Promise<$ReadOnlyArray<SettlementResult<number>>> = Promise.allSettled(generator());

p5.then((results): Array<number> => {
return results.map(result => {
if (result.status === 'fulfilled') {
return result.value;
} else {
return -1;
}
});
});
p5.then((results): Array<number> => {
return results.map(result => {
if (result.status === 'rejected') {
return -1;
} else {
return result.value
}
});
});
},
// wrong arguments
function() {
let p1 = Promise.allSettled();
let p2 = Promise.allSettled(1);
let p3 = Promise.allSettled(Promise.resolve(1));
},
// wrong result type
function() {
let p1 : Promise<void> = Promise.allSettled([]);
let p2 : Promise<[number, string, boolean]> = Promise.allSettled([1, '', false]);
let p3 : Promise<[number, string, boolean]> = Promise.allSettled([
Promise.resolve(1),
Promise.resolve(''),
Promise.resolve(false),
]);
},
// wrong result handling
function() {
let p1 = Promise.allSettled(generator());

p1.then((results): Array<number> => {
return results.map(result => result);
});

p1.then((results): Array<number> => {
return results.map(result => result.value);
});
},
];