Stage 1 Draft / October 13, 2021

ES Array.fromAsync (2021)

Introduction

This is the formal specification for a proposed Array.fromAsync factory method in JavaScript. It modifies the original ECMAScript specification with several new or revised clauses. See the proposal's explainer for the proposal's background, motivation, and usage examples.

1 Indexed Collections

1.1 Array Objects

1.1.1 Properties of the Array Constructor

1.1.1.1 Array.fromAsync ( asyncItems [ , mapfn [ , thisArg ] ] )

Editor's Note

This section is a wholly new subsection of the original Properties of the Array Constructor clause, to be inserted before the Array.from clause.

When the fromAsync method is called, the following steps are taken:

  1. Let C be the this value.
  2. Let promiseCapability be ! NewPromiseCapability(%Promise%).
  3. Let fromAsyncClosure be a new Abstract Closure with no parameters that captures C and mapfn and performs the following steps when called:
    1. If mapfn is undefined, let mapping be false.
    2. Else,
      1. If IsCallable(mapfn) is false, throw a TypeError exception.
      2. Let mapping be true.
    3. Let usingAsyncIterator be ? GetMethod(asyncItems, @@asyncIterator).
    4. If usingAsyncIterator is undefined,
      1. Let usingSyncIterator be ? GetMethod(asyncItems, @@iterator).
      2. If usingSyncIterator is undefined, set usingSyncIterator to %Array.prototype.values%.
    5. If IsConstructor(C) is true, then
      1. Let A be ? Construct(C).
    6. Else,
      1. Let A be ! ArrayCreate(0).
    7. If usingAsyncIterator is not undefined, then
      1. Let iteratorRecord be ? GetIterator(asyncItems, async, usingAsyncIterator).
    8. Else,
      1. Let iteratorRecord be ? CreateAsyncFromSyncIterator(GetIterator(syncItems, sync, usingSyncIterator)).
    9. Let k be 0.
    10. Repeat,
      1. If k ≥ 253 - 1, then
        1. Let error be ThrowCompletion(a newly created TypeError object).
        2. Return ? AsyncIteratorClose(iteratorRecord, error).
      2. Let Pk be ! ToString(𝔽(k)).
      3. Let next be ? Await(IteratorStep(iteratorRecord)).
      4. If next is false, then
        1. Perform ? Set(A, "length", 𝔽(k), true).
        2. Return A.
      5. Let nextValue be ? IteratorValue(next).
      6. If mapping is true, then
        1. Let mappedValue be Call(mapfn, thisArg, « nextValue, 𝔽(k) »).
        2. IfAbruptCloseAsyncIterator(iteratorRecord, mappedValue).
        3. Let mappedValue to Await(mappedValue).
        4. IfAbruptCloseAsyncIterator(iteratorRecord, mappedValue).
        5. Set mappedValue to mappedValue.[[Value]].
      7. Else, let mappedValue be nextValue.
      8. Let defineStatus be CreateDataPropertyOrThrow(A, Pk, mappedValue).
      9. If defineStatus is an abrupt completion, return ? AsyncIteratorClose(iteratorRecord, defineStatus).
      10. Set k to k + 1.
  4. Perform AsyncFunctionStart(promiseCapability, fromAsyncClosure).
  5. Return Completion { [[Type]]: return, [[Value]]: promiseCapability.[[Promise]], [[Target]]: empty }.
Note

The fromAsync function is an intentionally generic factory method; it does not require that its this value be the Array constructor. Therefore it can be transferred to or inherited by any other constructors that may be called with a single numeric argument.

1.2 TypedArray Objects

1.2.1 Properties of the %TypedArray% Intrinsic Object

1.2.1.1 %TypedArray%.fromAsync ( source [ , mapfn [ , thisArg ] ] )

Editor's Note

This section is a wholly new subsection of the original Properties of the %TypedArray% Intrinsic Object clause, to be inserted before the %TypedArray%.from clause.

When the fromAsync method is called, the following steps are taken:

  1. Let C be the this value.
  2. Let promiseCapability be ! NewPromiseCapability(%Promise%).
  3. Let fromAsyncClosure be a new Abstract Closure with no parameters that captures C and mapfn and performs the following steps when called:
    1. If IsConstructor(C) is false, throw a TypeError exception.
    2. If mapfn is undefined, let mapping be false.
    3. Else,
      1. If IsCallable(mapfn) is false, throw a TypeError exception.
      2. Let mapping be true.
    4. Let usingIterator be ? GetMethod(source, @@iterator).
    5. If usingIterator is not undefined, then
      1. Let values be ? IterableToList(source, usingIterator).
      2. Let len be the number of elements in values.
      3. Let targetObj be ? TypedArrayCreate(C, « 𝔽(len) »).
      4. Let k be 0.
      5. Repeat, while k < len,
        1. Let Pk be ! ToString(𝔽(k)).
        2. Let kValue be the first element of values and remove that element from values.
        3. If mapping is true, then
          1. Let mappedValue be ? Call(mapfn, thisArg, « kValue, 𝔽(k) »).
        4. Else, let mappedValue be kValue.
        5. Perform ? Set(targetObj, Pk, mappedValue, true).
        6. Set k to k + 1.
      6. Assert: values is now an empty List.
      7. Return targetObj.
    6. NOTE: source is not an Iterable so assume it is already an array-like object.
    7. Let arrayLike be ! ToObject(source).
    8. Let len be ? LengthOfArrayLike(arrayLike).
    9. Let targetObj be ? TypedArrayCreate(C, « 𝔽(len) »).
    10. Let k be 0.
    11. Repeat, while k < len,
      1. Let Pk be ! ToString(𝔽(k)).
      2. Let kValue be ? Get(arrayLike, Pk).
      3. If mapping is true, then
        1. Let mappedValue be ? Call(mapfn, thisArg, « kValue, 𝔽(k) »).
      4. Else, let mappedValue be kValue.
      5. Perform ? Set(targetObj, Pk, mappedValue, true).
      6. Set k to k + 1.
    12. Return targetObj.
  4. Perform AsyncFunctionStart(promiseCapability, fromAsyncClosure).
  5. Return Completion { [[Type]]: return, [[Value]]: promiseCapability.[[Promise]], [[Target]]: empty }.

A Copyright & Software License

Copyright Notice

© 2021 J. S. Choi, Ecma International

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.