Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Array.fromAsync() method

JavaScript Array.fromAsync() method

Array Object Reference JavaScript Array object


Example

Create an array from an asynchronous iterable:

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]

Try it »


Definition and Usage

Array.fromAsync() is a relatively new static method in JavaScript that allows you to create a new array from an asynchronous iterable, an iterable, or an array-like object. This method returns a Promise that resolves to a new array instance.

Simply put, it is the asynchronous version of Array.from(), specifically designed to handle asynchronous data sources.


Basic Syntax

Array

.

fromAsync

(

asyncIterable

)

Array

.

fromAsync

(

iterable

)

Array

.

fromAsync

(

arrayLike

)

Array

.

fromAsync

(

asyncIterable

,

mapFn

)

Array

.

fromAsync

(

asyncIterable

,

mapFn

,

thisArg

)

Parameter Description

Return Value

Returns a Promise that resolves to a new array.


Browser support

Array.fromAsync() is a relatively new feature that is currently (as of 2023) only supported in newer versions of browsers:

For unsupported environments, you can use a polyfill or implement similar functionality yourself.

The numbers in the table indicate the first browser version that supports this method.

Method
fromAsync() 117+ 121+ 115+ 16.4+ 81.0+

Usage examples

Example 1: Create an array from an async generator

Example

async

function

asyncGenerator

(

)

{

  yield

1

;

  yield

2

;

  yield

3

;

}

(

async

(

)

=>

{

 

const

result

=

await

Array

.

fromAsync

(

asyncGenerator

(

)

)

;

  console.

log

(

result

)

;

// Output: [1, 2, 3]

}

)

(

)

;

Example 2: Create an array from a regular iterable

Example

const

set

=

new

Set

(

[

1

,

2

,

3

]

)

;

(

async

(

)

=>

{

 

const

result

=

await

Array

.

fromAsync

(

set

)

;

  console.

log

(

result

)

;

// Output: [1, 2, 3]

}

)

(

)

;

Example 3: Use a mapping function

Example

async

function

asyncNumbers

(

)

{

  yield

1

;

  yield

2

;

  yield

3

;

}

(

async

(

)

=>

{

 

const

result

=

await

Array

.

fromAsync

(

asyncNumbers

(

)

,

x

=>

x

2

)

;

  console.

log

(

result

)

;

// Output: [2, 4, 6]

}

)

(

)

;


Difference from Array.from()

Although Array.fromAsync() and Array.from() look very similar, they have several key differences:

1. Handling asynchronous data sources

Array.fromAsync() can handle asynchronous iterables, while Array.from() cannot.

2. Return value

Array.from() returns an array directly, while Array.fromAsync() returns a Promise.

3. Execution timing

Array.from() executes synchronously, while Array.fromAsync() executes asynchronously.


Practical Application Scenarios

Scenario 1: Handling paginated API data

Example

async

function

paginatedFetcher

(

)

{

  let page

=

1

;

  while

(

true

)

{

   

const

response

=

await fetch

(

`https

:

//api.example.com/data?page=${page}`);

   

const

data

=

await response.

json

(

)

;

   

if

(

data.

length

===

0

)

break

;

    yield

data

;

    page

++;

 

}

}

(

async

(

)

=>

{

 

const

allData

=

await

Array

.

fromAsync

(

paginatedFetcher

(

)

)

;

  console.

log

(

allData

)

;

// Merge data from all pages into one array

}

)

(

)

;

Scenario 2: Handling the results of multiple asynchronous operations

Example

const

promises

=

[

  Promise.

resolve

(

1

)

,

  Promise.

resolve

(

2

)

,

  Promise.

resolve

(

3

)

]

;

(

async

(

)

=>

{

 

const

results

=

await

Array

.

fromAsync

(

promises

)

;

  console.

log

(

results

)

;

// Output: [1, 2, 3]

}

)

(

)

;


Summary

Array.fromAsync() is a powerful tool, especially suitable for handling asynchronous data collections. It simplifies the process of creating arrays from asynchronous data sources, making the code clearer and easier to maintain. Although browser support is not yet widespread, it will become one of the standard methods for handling asynchronous data collections over time.

Remember, when you need to create an array from an asynchronous data source, Array.fromAsync() is your good helper!

Array Object Reference JavaScript Array object