-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accept non-iterator array-like inputs, like Array.from? #7
Comments
It absolutely should accept them. |
Iterators helpers and collections |
I don’t think iterator helpers need to accept non-iterator array-like inputs. |
|
Oh, ah, right, I had forgotten about those. Well, yes, those might want to match |
I'm not sure that handling non-iterables should be added to all |
It's very much not obsolete, and it's very nice that things aren't forced to implement the iterator protocol to be transformable into an Array. This proposal must match |
How many new features are array-like and not iterable? The iterable protocol is the standard for handling collections. It's a bad practice not to use iterators protocol where it can be used. It's not hard to implement iterators protocol on a userland array-like - just add
It was decided not to add this argument to new features. It's added only to new completely similar to old features. For example, this argument was not added to
It's like to say that new array prototype methods should not handle holes as |
There's a difference - holes are bad and should never have been a thing, arraylikes are fine and have use cases beyond iteration. |
Array-like objects are fine, but for interaction with modern infrastructure, they should be also iterable. For handling legacy non-iterable array-like objects already available sync |
I agree with you there. The sole purpose of these |
Let it be. But let's return to this:
|
I guess I'm OK with it, but I'm not totally convinced. |
Yeah, I think it would just return a promise that resolves to the array that would have been created by
I imagine people will be occasionally passing sync iterables to Also, I created tc39/proposal-iterator-helpers#155. |
I'm not at all convinced by the consistency argument. This is an async method, which is a fairly different beast. It is not obvious to me why it should accept all of the synchronous inputs that its synchronous sibling does. |
The problem in #4 + #6, in this case: await Array.fromAsync([Promise.resolve(1), 2, Promise.resolve(3)]); // => [1, 2, 3]
await Array.fromAsync({ 0: Promise.resolve(1), 1: 2, 2: Promise.resolve(3), length: 3 }); // => [Promise.resolve(1), 2, Promise.resolve(3)] |
I don’t have any strong opinion either way. But it seems like there is significant disagreement within TC39 about this issue. What would be the best way to resolve this: keeping it in this issue, talking about it at plenary, an incubator call, or all of these? |
Well, so far you've only heard from two committee members, so it's hard to say what the opinion of the committee as a whole would be. It's hard to get a broader sense of opinions without raising it in plenary, so that's probably what I'd do as the next step. |
I've been playing around with the Gzemnid dataset for the top 1000 downloaded NPM packages (basically it's a large text file containing all of their source code), and have been trying to find relevant information for another proposal. @js-choi asked me to see if I could also find anything relevant to this thread from the dataset as well. Searching the dataset for usages of To find anything relevant to this issue, I decided to look for anything that had test('passes through arguments', (t) => {
const rateLimited = rateLimit(10, 100, resolveArguments);
rateLimited('a', 'b', 'c').then((res) => {
t.deepEqual(res, ['a', 'b', 'c']);
t.end();
});
function resolveArguments() {
return Promise.resolve([].slice.call(arguments));
}
}); They had created a helper "resolveArguments()" function that took an arbitrary list of arguments, turned them into an array, then returned that array as a promise, because they were trying to stub an async callback. Today, that helper would probably be written simply like this: I also tried checking for any scenario with Promise.resolve() was on the line next to the arrayLike-to-array operation, in case they split up the logic over multiple lines, but I didn't find anything relevant with that search. Of course, it's very possible people that many others were trying to do this kind of logic, but they had simply split it up over many lines, like this:
Or, it's possible that people were doing this sort of logic through other means that I wasn't searching for at all. If anyone else is interested in looking at some of this data themselves, I can share the first few thousand occurrences of |
Above is the raw data I could find, now I'll share a couple of my opinions on the matter. From what I can tell, it seems like turning an arrayLike into a promise isn't an extremely common operation, but I'm sure there's the occasional use for it (I just couldn't find any great examples from the dataset using the blunt, text-searching tools I was using). So, usability-wise, supporting ArrayLike with Array.fromAsync() probably isn't that important, especially since it's not that hard to just do So, it's probably best to do whatever feels the most consistent, and what you think programmers would expect. I get the argument that Though, I'm a little torn. I don't know what developers would actually expect. update: On second thought, I wonder if I'm been looking for the wrong thing. First, I took a closer look at the proposal, and saw that |
An arraylike would be treated exactly the same as an array; the language primarily accepts an arraylike anywhere an array is accepted, except for places that only accept iterables. |
I made a brief update presentation about this issue to the Committee at the October plenary today. Although feedback time was but brief, I got no clear signals of opposition to making Array.fromAsync’s inputs be a superset of Array.from’s. @syg asked whether each item from the array-like input would be awaited. I confirmed yes, since that’s what would happen with a non-async sync iterable (just like with I plan to move forward with allowing non-iterator array-like inputs when I present this proposal again for Stage 2 in a few months. |
Reached Stage 2 today, with the answer to this question as yes. |
Array.from accepts non-iterable array-like inputs (objects with a length property and indexed elements).
Should the inputs of Array.fromAsync be a superset of Array.from?
The text was updated successfully, but these errors were encountered: