-
Notifications
You must be signed in to change notification settings - Fork 165
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
Async constructors? (new Foo()
returning Promise<Foo>)
#563
Comments
So to be clear, the usage would be something like this? const sheet = await new StyleSheet(text); The syntax is nice and clean in isolation. Questions that occur to me:
|
See also w3ctag/design-principles#44. |
Yes, exactly. (Or munging several into a Promise.all(), etc.)
I suspect we'd want to make that restriction, yes. "Multiple" constructors are just a syntax nicety for doing type-checking over complicated argument lists; they're all implemented as a single JS function. If we pretend that this function is designated
Dunno! This should be investigated. |
While return new StyleSheet(text).then(actualSheet => actualSheet.doSomethingElse()); In general I would prefer preserving the |
I don't think this is a good idea. Even though JavaScript is flexible enough to do otherwise, a constructor should return an instance of the class being constructed. ie, |
Sounds like we have a rough consensus on the direction here, but glad we talked it through. Let's close :) |
Per the current spec:
This means that if you want to only provide an async way to obtain an object, you can't use a constructor - you have to omit the constructor (so it default-throws), and then use a factory function that actually constructs the object.
Is there any appetite for being able to mark a constructor as async in some way, such that it can return a Promise for the instance?
The text was updated successfully, but these errors were encountered: