-
Notifications
You must be signed in to change notification settings - Fork 4
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
overload Promise.all
#13
Comments
An additional con is that if they're passing a non-iterable right now it's almost certainly a bug, but this would mask it. For example: iow, this feels like a nonstarter to me. |
Yeah, this is begging for trouble. |
MM also suggested the same issue, should we reopen? |
@erights' suggestion was a bit different: basically saying "if the first arg is not iterable and arguments.length > 1, throw", and then the fallback would be to accept the lone non-iterable object argument and return an object. |
Generally I like be strict on argument length, and it seems solve the misuse problem in this specific issue. |
Not just I favor that it operates on exactly the enumerable own properties, whether string-named or symbol-named, like Nice uniformity across the two overloads because, for a normal array, all the numeric indexed properties are also enumerable own. |
This is definitely the way to go when it comes to designing a nice API that is going to be a joy to use. We already have a bunch of ugly names in JavaScript, and we even have a precedent for trying to fix them, like The method should always take a single argument so that when someone forgets |
Pretty much every developer now a day uses some IDE. Given that the IDE will complain that the function got too many arguments. I would say that in the modern day, this complaint is no longer valid. |
I don't use an IDE, and neither does every developer. The complaint will forever be valid. |
That's why there is Pretty much every and not every... What do you use, then? (I am curious)
Both complaints will be forever valid. The point of this discussion is to determine if the benefit of putting an object to this function would ease more people than it would hurt. Pros:
Cons:
|
I don't think a separate name is confusing to anyone; arguably, IDE autocomplete means it would be less confusing - that said, if we decide on a way to safely overload the existing mechanism, that's nice. |
Personally I very much like the idea of overloading the existing methods. As mentioned there is the risk of
One hypothetical way to further reduce the risk would be to only accept iterables or objects with a prototype set to It would be great to get as much feedback from developers with a diverse range of backgrounds on how their would find the overload vs separate dedicated APIs. |
Technically you could create an object with an iterator like so:
If a user does this, it is done by design... The risk, therefore, arising from this is minimal. However, this (or any similar) example should be documented in the proposal. May I suggest a poll? (vote for both if you please) |
After a few weeks, my passion for opposing this has cooled a bit. I care more about a workable solution than how that solution is implemented. That said, I'm thinking also about type definitions from TypeScript, which gives me another concern. The current type definitions in TypeScript for // from es2015.iterable.d.ts
all<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>[]>;
// from es2015.promise.d.ts
all<T extends readonly unknown[] | []>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]> }>; https://github.com/microsoft/TypeScript/blob/main/src/lib/es2015.iterable.d.ts What would the types look like when overloaded? |
@ajvincent Presumably an extra overload along the lines of all<T extends { [key: string]: unknown }>(obj: T): Promise<{ [P in keyof T]: Awaited<T[P]> }> ? |
Since the array has |
if overloading, we would be overloading all of the Promise concurrency methods, right? |
Both Promise.all and Promise.allSettled, I'd expect. |
ah right, this wouldn't make much sense for |
There could be an overload for them... const result = await Promise.race({
a: new Promise(() => {}), // never resolves - always loses the race
b: Promise.resolve(42),
});
result; // { b: 42 }
"a" in result; // false
"b" in result; // true However I agree that I don't think we should pursue this design because |
We must be consistent with the current API. |
The consistency here is that the overload is available when the method returns a container. To pass an object to |
True the benefit added by overload of |
Consider currently
Promise.all({})
just throw, practically we could overloadPromise.all
:Pros: No naming issue (
Promise.allOwnProperties()
is too lengthy)Cons: Theoretically, people could add
Object.prototype[Symbol.iterator]
and change the behavior.The text was updated successfully, but these errors were encountered: