Skip to content

Commit

Permalink
feat: enable Array.fromAsync (#21048)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored Nov 1, 2023
1 parent 24c3c96 commit ab72019
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions cli/tests/unit/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}`,
);
Expand Down
17 changes: 17 additions & 0 deletions cli/tests/unit/globals_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);
});
14 changes: 14 additions & 0 deletions cli/tsc/dts/lib.esnext.array.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
iterableOrArrayLike: AsyncIterable<T> | Iterable<T | Promise<T>> | ArrayLike<T | Promise<T>>,
): Promise<T[]>;

fromAsync<T, U>(
iterableOrArrayLike: AsyncIterable<T> | Iterable<T> | ArrayLike<T>,
mapFn: (value: Awaited<T>) => U,
thisArg?: any,
): Promise<Awaited<U>[]>;
}

0 comments on commit ab72019

Please sign in to comment.