Skip to content

Commit ab72019

Browse files
authored
feat: enable Array.fromAsync (#21048)
1 parent 24c3c96 commit ab72019

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

cli/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ pub fn main() {
368368
// Using same default as VSCode:
369369
// https://github.com/microsoft/vscode/blob/48d4ba271686e8072fc6674137415bc80d936bc7/extensions/typescript-language-features/src/configuration/configuration.ts#L213-L214
370370
DenoSubcommand::Lsp => vec!["--max-old-space-size=3072".to_string()],
371-
_ => vec![],
371+
// TODO(bartlomieju): upstream this to `deno_core` crate
372+
_ => vec!["--harmony-array-from-async".to_string()],
372373
};
373374
init_v8_flags(&default_v8_flags, &flags.v8_flags, get_v8_flags_from_env());
374375
deno_core::JsRuntime::init_platform(None);

cli/tests/unit/console_test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ Deno.test(function consoleTestStringifyFunctionWithProperties() {
500500
[isArray]: [Function: isArray] { [length]: 1, [name]: "isArray" },
501501
[from]: [Function: from] { [length]: 1, [name]: "from" },
502502
[of]: [Function: of] { [length]: 0, [name]: "of" },
503+
[fromAsync]: [Function: fromAsync] { [length]: 1, [name]: "fromAsync" },
503504
[Symbol(Symbol.species)]: [Getter]
504505
}`,
505506
);

cli/tests/unit/globals_test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,20 @@ Deno.test(async function promiseWithResolvers() {
153153
await assertRejects(() => promise, Error, "boom!");
154154
}
155155
});
156+
157+
Deno.test(async function arrayFromAsync() {
158+
// Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fromAsync#examples
159+
// Thank you.
160+
const asyncIterable = (async function* () {
161+
for (let i = 0; i < 5; i++) {
162+
await new Promise((resolve) => setTimeout(resolve, 10 * i));
163+
yield i;
164+
}
165+
})();
166+
167+
const a = await Array.fromAsync(asyncIterable);
168+
assertEquals(a, [0, 1, 2, 3, 4]);
169+
170+
const b = await Array.fromAsync(new Map([[1, 2], [3, 4]]));
171+
assertEquals(b, [[1, 2], [3, 4]]);
172+
});

cli/tsc/dts/lib.esnext.array.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,17 @@ interface BigUint64Array {
291291

292292
with(index: number, value: number): BigUint64Array;
293293
}
294+
295+
// NOTE(bartlomieju): taken from https://github.com/microsoft/TypeScript/issues/50803#issuecomment-1249030430
296+
// while we wait for these types to officially ship
297+
interface ArrayConstructor {
298+
fromAsync<T>(
299+
iterableOrArrayLike: AsyncIterable<T> | Iterable<T | Promise<T>> | ArrayLike<T | Promise<T>>,
300+
): Promise<T[]>;
301+
302+
fromAsync<T, U>(
303+
iterableOrArrayLike: AsyncIterable<T> | Iterable<T> | ArrayLike<T>,
304+
mapFn: (value: Awaited<T>) => U,
305+
thisArg?: any,
306+
): Promise<Awaited<U>[]>;
307+
}

0 commit comments

Comments
 (0)