JavaScript Tutorials

JavaScript Array.fromAsync()
Syntax & Examples

Array.fromAsync() static-method

The Array.fromAsync() method in JavaScript creates a new Array instance from an async iterable, iterable, or array-like object.


Syntax of Array.fromAsync()

There are 3 variations for the syntax of Array.fromAsync() static-method. They are:

1.
Array.fromAsync(arrayLike)

Parameters

ParameterOptional/RequiredDescription
arrayLikerequiredAn async iterable, iterable, or array-like object to convert to an array.

This static-method creates a new Array instance from an async iterable, iterable, or array-like object.

Returns value of type Promise<Array>.

2.
Array.fromAsync(arrayLike, mapFn)

Parameters

ParameterOptional/RequiredDescription
arrayLikerequiredAn async iterable, iterable, or array-like object to convert to an array.
mapFnoptionalA function to call on each element of the array.

This static-method creates a new Array instance from an async iterable, iterable, or array-like object, using a map function to process each element.

Returns value of type Promise<Array>.

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

Parameters

ParameterOptional/RequiredDescription
arrayLikerequiredAn async iterable, iterable, or array-like object to convert to an array.
mapFnoptionalA function to call on each element of the array.
thisArgoptionalA value to use as this when executing the map function.

This static-method creates a new Array instance from an async iterable, iterable, or array-like object, using a map function to process each element and a thisArg to use as this when executing the map function.

Returns value of type Promise<Array>.



✐ Examples

1 Using Array.fromAsync() method with an async iterable

In JavaScript, we can use the Array.fromAsync() method to create a new array from an async iterable.

For example,

  1. We define an async generator function asyncGenerator that yields three values.
  2. We use the Array.fromAsync() method with asyncGenerator to create a new array from the yielded values.
  3. The result is stored in the variable asyncArray.
  4. We log asyncArray to the console using console.log() method to see the created array.

JavaScript Program

async function* asyncGenerator() {
  yield 1;
  yield 2;
  yield 3;
}
Array.fromAsync(asyncGenerator()).then(asyncArray => console.log(asyncArray));

Output

[1, 2, 3]

2 Using Array.fromAsync() method with a map function

We can use the Array.fromAsync() method to create a new array from an async iterable, using a map function to process each element.

For example,

  1. We define an async generator function asyncGenerator that yields three values.
  2. We define a map function double that doubles the value of each element.
  3. We use the Array.fromAsync() method with asyncGenerator and double to create a new array with the doubled values.
  4. The result is stored in the variable asyncArray.
  5. We log asyncArray to the console using console.log() method to see the created array with the doubled values.

JavaScript Program

async function* asyncGenerator() {
  yield 1;
  yield 2;
  yield 3;
}
const double = x => x * 2;
Array.fromAsync(asyncGenerator(), double).then(asyncArray => console.log(asyncArray));

Output

[2, 4, 6]

3 Using Array.fromAsync() method with a map function and thisArg

We can use the Array.fromAsync() method to create a new array from an async iterable, using a map function to process each element and a thisArg to use as this when executing the map function.

For example,

  1. We define an async generator function asyncGenerator that yields three values.
  2. We define an object context with a multiplier property set to 3.
  3. We define a map function multiply that multiplies the value of each element by the multiplier property of the context object.
  4. We use the Array.fromAsync() method with asyncGenerator, multiply, and context to create a new array with the multiplied values.
  5. The result is stored in the variable asyncArray.
  6. We log asyncArray to the console using console.log() method to see the created array with the multiplied values.

JavaScript Program

async function* asyncGenerator() {
  yield 1;
  yield 2;
  yield 3;
}
const context = { multiplier: 3 };
function multiply(x) {
  return x * this.multiplier;
}
Array.fromAsync(asyncGenerator(), multiply, context).then(asyncArray => console.log(asyncArray));

Output

[3, 6, 9]

Summary

In this JavaScript tutorial, we learned about fromAsync() static-method of Array: the syntax and few working examples with output and detailed explanation for each example.