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:
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>
.
✐ 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,
- We define an async generator function
asyncGenerator
that yields three values. - We use the
Array.fromAsync()
method withasyncGenerator
to create a new array from the yielded values. - The result is stored in the variable
asyncArray
. - We log
asyncArray
to the console usingconsole.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,
- We define an async generator function asyncGenerator that yields three values.
- We define a map function double that doubles the value of each element.
- We use the Array.fromAsync() method with asyncGenerator and double to create a new array with the doubled values.
- The result is stored in the variable asyncArray.
- 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,
- We define an async generator function asyncGenerator that yields three values.
- We define an object context with a multiplier property set to 3.
- We define a map function multiply that multiplies the value of each element by the multiplier property of the context object.
- We use the Array.fromAsync() method with asyncGenerator, multiply, and context to create a new array with the multiplied values.
- The result is stored in the variable asyncArray.
- 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.