Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Should Iterator.from and AsyncIterator.from support non-iterable arraylikes #231

Closed
js-choi opened this issue Sep 8, 2022 · 1 comment
Closed

Comments

@js-choi
Copy link

js-choi commented Sep 8, 2022

Array.from supports non-iterable “arraylike” objects: objects that have integer properties and a length property but no Symbol.iterator property. For example, Array.from({ 0: 'a', 1: 'b', 2: 'c', length: 2 }) returns [ 'a', 'b' ].

Array.fromAsync is also going to support non-iterable “arraylike” objects. For example, await Array.fromAsync({ 0: Promise.resolve('a'), 1: Promise.resolve('b'), 2: Promise.resolve('c'), length: 2 }) returns [ 'a', 'b' ].

Array.fromAsync(input) is designed to be equivalent to AsyncIterator.from(input).toArray().
This comes from how Array.from(input) would be equivalent to Iterator.from(input).toArray().

However, they are not actually equivalent, because Iterator.from(input) and AsyncIterator.from(input) do not support non-iterable arraylike inputs. Should they? (I don’t have a strong opinion except that an explicit decision should be made.)

@bakkot
Copy link
Collaborator

bakkot commented Sep 8, 2022

No, they should not. Array.from (and I guess its async version) should be the one and only way of consuming arraylikes. It's fine for Array.fromAsync and AsyncIterator.from(input).toArray() to differ in this way.

@michaelficarra michaelficarra closed this as not planned Won't fix, can't repro, duplicate, stale Sep 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants