array-from-async

3.0.0 • Public • Published

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.

Package Sidebar

Install

npm i array-from-async

Weekly Downloads

2,049

Version

3.0.0

License

BSD-3-Clause

Unpacked Size

43.4 kB

Total Files

9

Last publish

Collaborators

  • ljharb
  • js-choi