Skip to content

es-shims/array-from-async

Array.fromAsync polyfill

TC39 is considering a new standard Array.fromAsync convenience method. This is a standalone polyfill for that.

Requirements

This polyfill requires ES 2018 (which must include support for async iterators).

Installation

In Node:

npm install array-from-async

…then:

import fromAsync from 'array-from-async';

In web browsers or Deno:

import fromAsync from '//unpkg.com/array-from-async';

Description

This is a standalone implementation of the proposed Array.fromAsync function. Unlike typical polyfills/shims, it does not mutate the global Array constructor; instead, it exports an equivalent function.

Similarly to Array.from, fromAsync would be a static method of the Array built-in class, with one required argument and two optional arguments: (items, mapfn, thisArg).

But instead of converting an iterable to an array, it converts an async iterable to a promise that will resolve to an array.

async function * f () {
  for (let i = 0; i < 4; i++)
    yield i * 2;
}

// Resolves to `[0, 2, 4, 6]`.
await fromAsync(f());

For more information, see the Array.fromAsync explainer.

About

An implementation for Array.fromAsync, which dumps an async iterator into an array.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks