From ab72019a17ccc077384c37eaeae15fffc9ce4d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 1 Nov 2023 23:25:18 +0100 Subject: [PATCH] feat: enable Array.fromAsync (#21048) --- cli/main.rs | 3 ++- cli/tests/unit/console_test.ts | 1 + cli/tests/unit/globals_test.ts | 17 +++++++++++++++++ cli/tsc/dts/lib.esnext.array.d.ts | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cli/main.rs b/cli/main.rs index 24b964169c..c6249a21f1 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -368,7 +368,8 @@ pub fn main() { // Using same default as VSCode: // https://github.com/microsoft/vscode/blob/48d4ba271686e8072fc6674137415bc80d936bc7/extensions/typescript-language-features/src/configuration/configuration.ts#L213-L214 DenoSubcommand::Lsp => vec!["--max-old-space-size=3072".to_string()], - _ => vec![], + // TODO(bartlomieju): upstream this to `deno_core` crate + _ => vec!["--harmony-array-from-async".to_string()], }; init_v8_flags(&default_v8_flags, &flags.v8_flags, get_v8_flags_from_env()); deno_core::JsRuntime::init_platform(None); diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index d8990559e9..d6e2a5263d 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -500,6 +500,7 @@ Deno.test(function consoleTestStringifyFunctionWithProperties() { [isArray]: [Function: isArray] { [length]: 1, [name]: "isArray" }, [from]: [Function: from] { [length]: 1, [name]: "from" }, [of]: [Function: of] { [length]: 0, [name]: "of" }, + [fromAsync]: [Function: fromAsync] { [length]: 1, [name]: "fromAsync" }, [Symbol(Symbol.species)]: [Getter] }`, ); diff --git a/cli/tests/unit/globals_test.ts b/cli/tests/unit/globals_test.ts index e5ff2cc8e8..b47e83cfd3 100644 --- a/cli/tests/unit/globals_test.ts +++ b/cli/tests/unit/globals_test.ts @@ -153,3 +153,20 @@ Deno.test(async function promiseWithResolvers() { await assertRejects(() => promise, Error, "boom!"); } }); + +Deno.test(async function arrayFromAsync() { + // Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fromAsync#examples + // Thank you. + const asyncIterable = (async function* () { + for (let i = 0; i < 5; i++) { + await new Promise((resolve) => setTimeout(resolve, 10 * i)); + yield i; + } + })(); + + const a = await Array.fromAsync(asyncIterable); + assertEquals(a, [0, 1, 2, 3, 4]); + + const b = await Array.fromAsync(new Map([[1, 2], [3, 4]])); + assertEquals(b, [[1, 2], [3, 4]]); +}); diff --git a/cli/tsc/dts/lib.esnext.array.d.ts b/cli/tsc/dts/lib.esnext.array.d.ts index a74a7b3110..5bf6612fc6 100644 --- a/cli/tsc/dts/lib.esnext.array.d.ts +++ b/cli/tsc/dts/lib.esnext.array.d.ts @@ -291,3 +291,17 @@ interface BigUint64Array { with(index: number, value: number): BigUint64Array; } + +// NOTE(bartlomieju): taken from https://github.com/microsoft/TypeScript/issues/50803#issuecomment-1249030430 +// while we wait for these types to officially ship +interface ArrayConstructor { + fromAsync( + iterableOrArrayLike: AsyncIterable | Iterable> | ArrayLike>, + ): Promise; + + fromAsync( + iterableOrArrayLike: AsyncIterable | Iterable | ArrayLike, + mapFn: (value: Awaited) => U, + thisArg?: any, + ): Promise[]>; +}