The Array.fromAsync() method in JavaScript creates a new Array instance from an async iterable, iterable, or array-like object.
There are 3 variations for the syntax of Array.fromAsync() static-method. They are:
Array.fromAsync(arrayLike)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
arrayLike | required | An 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>
.
Array.fromAsync(arrayLike, mapFn)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
arrayLike | required | An async iterable, iterable, or array-like object to convert to an array. |
mapFn | optional | A 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>
.
Array.fromAsync(arrayLike, mapFn, thisArg)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
arrayLike | required | An async iterable, iterable, or array-like object to convert to an array. |
mapFn | optional | A function to call on each element of the array. |
thisArg | optional | A 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>
.
In JavaScript, we can use the Array.fromAsync() method to create a new array from an async iterable.
For example,
asyncGenerator
that yields three values.Array.fromAsync()
method with asyncGenerator
to create a new array from the yielded values.asyncArray
.asyncArray
to the console using console.log()
method to see the created array.async function* asyncGenerator() {
yield 1;
yield 2;
yield 3;
}
Array.fromAsync(asyncGenerator()).then(asyncArray => console.log(asyncArray));
[1, 2, 3]
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,
async function* asyncGenerator() {
yield 1;
yield 2;
yield 3;
}
const double = x => x * 2;
Array.fromAsync(asyncGenerator(), double).then(asyncArray => console.log(asyncArray));
[2, 4, 6]
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,
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));
[3, 6, 9]
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.