[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/fromAsync [Back]  [Original]

Array.fromAsync() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array.fromAsync()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2024 1.

Array.fromAsync() , , Array .

In this article

js
Array.fromAsync(arrayLike)
Array.fromAsync(arrayLike, mapFn)
Array.fromAsync(arrayLike, mapFn, thisArg)

arrayLike

, , .

mapFn Optional

. , (await ) mapFn . .

element

. await , then .

index

.

thisArg Optional

mapFn this .

Array Promise.

Array.fromAsync() .

Array.fromAsync() for await...of . Array.fromAsync() Array.from() .

  • Array.fromAsync() .
  • Array.fromAsync() Promise .
  • Array.fromAsync() await.
  • mapFn , await .

Array.fromAsync() Promise.all() . .

  • Array.fromAsync() yield await . Promise.all() await .
  • Array.fromAsync() (lazy) , . Promise.all() await .

js
const asyncIterable = (async function* () {
  for (let i = 0; i < 5; i++) {
    await new Promise((resolve) => setTimeout(resolve, 10 * i));
    yield i;
  }
})();

Array.fromAsync(asyncIterable).then((array) => console.log(array));
// [0, 1, 2, 3, 4]

js
Array.fromAsync(
  new Map([
    [1, 2],
    [3, 4],
  ]),
).then((array) => console.log(array));
// [[1, 2], [3, 4]]

yield

js
Array.fromAsync(
  new Set([Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]),
).then((array) => console.log(array));
// [1, 2, 3]

js
Array.fromAsync({
  length: 3,
  0: Promise.resolve(1),
  1: Promise.resolve(2),
  2: Promise.resolve(3),
}).then((array) => console.log(array));
// [1, 2, 3]

mapFn

mapFn Array.fromAsync() await .

js
function delayedValue(v) {
  return new Promise((resolve) => setTimeout(() => resolve(v), 100));
}

Array.fromAsync(
  [delayedValue(1), delayedValue(2), delayedValue(3)],
  (element) => delayedValue(element * 2),
).then((array) => console.log(array));
// [2, 4, 6]

Promise.all()

Array.fromAsync() yield await . Promise.all() await .

js
function* makeAsyncIterable() {
  for (let i = 0; i < 5; i++) {
    yield new Promise((resolve) => setTimeout(resolve, 100));
  }
}

(async () => {
  console.time("Array.fromAsync() ");
  await Array.fromAsync(makeAsyncIterable());
  console.timeEnd("Array.fromAsync() ");
  // Array.fromAsync() : 503.610ms

  console.time("Promise.all() ");
  await Promise.all(makeAsyncIterable());
  console.timeEnd("Promise.all() ");
  // Promise.all() : 101.728ms
})();

for await...of , , , return() .

js
function* generatorWithRejectedPromises() {
  try {
    yield 0;
    yield Promise.reject(3);
  } finally {
    console.log("finally ");
  }
}

(async () => {
  try {
    await Array.fromAsync(generatorWithRejectedPromises());
  } catch (e) {
    console.log(e, "");
  }
})();
// 3 
// "finally "  

, for...of , await .

js
(async () => {
  const arr = [];
  try {
    for (const val of generatorWithRejectedPromises()) {
      arr.push(await val);
    }
  } catch (e) {
    console.log(e, "");
  }
})();
// finally 
// 3 

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.fromasync


Web Proxy Viewer  |  New URL  |  Original Page