From 72ee5e09a6319ece35323ba145977e6d17047663 Mon Sep 17 00:00:00 2001 From: shau-kote <> Date: Sun, 15 Dec 2019 01:43:18 +0300 Subject: [PATCH 1/9] Add type signature for Promise.allSettled() --- lib/core.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/core.js b/lib/core.js index 2b576f7996e..feee746a147 100644 --- a/lib/core.js +++ b/lib/core.js @@ -688,12 +688,16 @@ declare class Promise<+R> { static resolve(object: Promise | T): Promise; static reject(error: any): Promise; static all>(promises: T): Promise<$TupleMap>; + static allSettled>(promises: T): Promise<$TupleMap>; static race | T>(promises: Iterable): Promise; } // we use this signature when typing await expressions declare function $await(p: Promise | T): T; +declare function $settlement(p: Promise | T): SettlementResult; +declare type SettlementResult = { status: 'fulfilled', value: T } | { status: 'rejected', reason: any }; + /* Binary data */ declare class ArrayBuffer { From cb3c239fc6afc556248baa420254ee078eaa0939 Mon Sep 17 00:00:00 2001 From: shau-kote <> Date: Sat, 7 Mar 2020 03:53:24 +0300 Subject: [PATCH 2/9] Make object types in SettlementResult exact --- lib/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core.js b/lib/core.js index feee746a147..e8725f56e77 100644 --- a/lib/core.js +++ b/lib/core.js @@ -696,7 +696,7 @@ declare class Promise<+R> { declare function $await(p: Promise | T): T; declare function $settlement(p: Promise | T): SettlementResult; -declare type SettlementResult = { status: 'fulfilled', value: T } | { status: 'rejected', reason: any }; +declare type SettlementResult = {| status: 'fulfilled', value: T |} | {| status: 'rejected', reason: any |}; /* Binary data */ From 56350b3195c3d0d43ef3e36909a5e2da45ae447e Mon Sep 17 00:00:00 2001 From: shau-kote <> Date: Sat, 7 Mar 2020 04:08:40 +0300 Subject: [PATCH 3/9] Replace 'any' in SettlementResult by 'mixed' --- lib/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core.js b/lib/core.js index e8725f56e77..a72689c87de 100644 --- a/lib/core.js +++ b/lib/core.js @@ -696,7 +696,7 @@ declare class Promise<+R> { declare function $await(p: Promise | T): T; declare function $settlement(p: Promise | T): SettlementResult; -declare type SettlementResult = {| status: 'fulfilled', value: T |} | {| status: 'rejected', reason: any |}; +declare type SettlementResult = {| status: 'fulfilled', value: T |} | {| status: 'rejected', reason: mixed |}; /* Binary data */ From dfc97b690a692ac938bac30fe346929999d72106 Mon Sep 17 00:00:00 2001 From: shau-kote <> Date: Sat, 7 Mar 2020 04:25:35 +0300 Subject: [PATCH 4/9] Make SettlementResult covariant --- lib/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core.js b/lib/core.js index a72689c87de..a6270a7c71a 100644 --- a/lib/core.js +++ b/lib/core.js @@ -696,7 +696,7 @@ declare class Promise<+R> { declare function $await(p: Promise | T): T; declare function $settlement(p: Promise | T): SettlementResult; -declare type SettlementResult = {| status: 'fulfilled', value: T |} | {| status: 'rejected', reason: mixed |}; +declare type SettlementResult<+T> = {| status: 'fulfilled', +value: T |} | {| status: 'rejected', reason: mixed |}; /* Binary data */ From 91e74a0c5419e81eb9f6fd01f738850e5cb43b39 Mon Sep 17 00:00:00 2001 From: shau-kote <> Date: Tue, 10 Mar 2020 04:23:43 +0300 Subject: [PATCH 5/9] Add test suite for good cases --- tests/core_tests/allSettled.js | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/core_tests/allSettled.js diff --git a/tests/core_tests/allSettled.js b/tests/core_tests/allSettled.js new file mode 100644 index 00000000000..1d64573a3dd --- /dev/null +++ b/tests/core_tests/allSettled.js @@ -0,0 +1,55 @@ +// @flow + +function* generator(): Iterable> { + while (true) { + yield Promise.resolve(1); + } +} + +let tests = [ + // good cases + function() { + let p1 : Promise<[]> = Promise.allSettled([]); + let p2 : Promise<[ + SettlementResult, + SettlementResult, + SettlementResult, + ]> = Promise.allSettled([1, '', false]); + let p3 : Promise<[ + SettlementResult, + SettlementResult, + SettlementResult, + ]> = Promise.allSettled([ + Promise.resolve(1), + Promise.resolve(''), + Promise.resolve(false), + ]); + let p4 : Promise<[ + SettlementResult, + SettlementResult, + ]> = Promise.allSettled([ + 1, + Promise.resolve(1), + ]); + let p5 : Promise<$ReadOnlyArray>> = Promise.allSettled(generator()); + + p5.then((results): Array => { + return results.map(result => { + if (result.status === 'fulfilled') { + return result.value; + } else { + return -1; + } + }); + }); + p5.then((results): Array => { + return results.map(result => { + if (result.status === 'rejected') { + return -1; + } else { + return result.value + } + }); + }); + }, +]; \ No newline at end of file From d929c014ae182419f3e27b33f56e1b19d3f376c8 Mon Sep 17 00:00:00 2001 From: shau-kote <> Date: Tue, 10 Mar 2020 06:07:10 +0300 Subject: [PATCH 6/9] Add test suite for cases with wrong args --- tests/core_tests/allSettled.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/core_tests/allSettled.js b/tests/core_tests/allSettled.js index 1d64573a3dd..6f95af782e3 100644 --- a/tests/core_tests/allSettled.js +++ b/tests/core_tests/allSettled.js @@ -52,4 +52,10 @@ let tests = [ }); }); }, + // wrong arguments + function() { + let p1 = Promise.allSettled(); + let p2 = Promise.allSettled(1); + let p3 = Promise.allSettled(Promise.resolve(1)); + }, ]; \ No newline at end of file From 0c4a12e7f8c8a1399d3f0de2707697bc29030bb2 Mon Sep 17 00:00:00 2001 From: shau-kote <> Date: Tue, 10 Mar 2020 06:10:47 +0300 Subject: [PATCH 7/9] Add test suite for cases with wrong result types --- tests/core_tests/allSettled.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/core_tests/allSettled.js b/tests/core_tests/allSettled.js index 6f95af782e3..4542b67e13a 100644 --- a/tests/core_tests/allSettled.js +++ b/tests/core_tests/allSettled.js @@ -58,4 +58,14 @@ let tests = [ let p2 = Promise.allSettled(1); let p3 = Promise.allSettled(Promise.resolve(1)); }, + // wrong result type + function() { + let p1 : Promise = 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), + ]); + }, ]; \ No newline at end of file From aa3d4304aa8138f3a4567eba48fdd768112cdd29 Mon Sep 17 00:00:00 2001 From: shau-kote <> Date: Tue, 10 Mar 2020 06:22:31 +0300 Subject: [PATCH 8/9] Add test suite for cases with wrong result handling --- tests/core_tests/allSettled.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/core_tests/allSettled.js b/tests/core_tests/allSettled.js index 4542b67e13a..413bc7bc531 100644 --- a/tests/core_tests/allSettled.js +++ b/tests/core_tests/allSettled.js @@ -68,4 +68,16 @@ let tests = [ Promise.resolve(false), ]); }, + // wrong result handling + function() { + let p1 = Promise.allSettled(generator()); + + p1.then((results): Array => { + return results.map(result => result); + }); + + p1.then((results): Array => { + return results.map(result => result.value); + }); + }, ]; \ No newline at end of file From f792f2115dbd17f986469f320454e3d5231b9b5a Mon Sep 17 00:00:00 2001 From: shau-kote <> Date: Tue, 10 Mar 2020 06:37:34 +0300 Subject: [PATCH 9/9] Add trailing newline --- tests/core_tests/allSettled.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core_tests/allSettled.js b/tests/core_tests/allSettled.js index 413bc7bc531..fa3f8b6b3da 100644 --- a/tests/core_tests/allSettled.js +++ b/tests/core_tests/allSettled.js @@ -80,4 +80,4 @@ let tests = [ return results.map(result => result.value); }); }, -]; \ No newline at end of file +];