-
Notifications
You must be signed in to change notification settings - Fork 10
Does it support async-await? #7
Comments
At the moment, no. The only way I could see that be feasible is if the ability to |
Currently both |
@rbuckton What's the reason for this restriction? It's surprising to me. |
@rbuckton ping^ |
To reduce complexity. IIRC, we've discussed in committee the possibility of opening this up later, and we've added (and are adding) restrictions so that we can open this back up in the future. Currently, allowing There are a number of directions we can go for this that we don't have to decide now, so I've focused on shipping a MVP solution that we can expand upon in the future. Multi-step static initialization with access to private state is something I'm seeing is needed now since private fields is already shipping in NodeJS. I've already seen in-the-wild examples of people abusing computed property names to try to achieve this, and that approach is clunky and hard to read or maintain. Supporting |
Hm, OK. I guess I didn't imagine there was any question about what the semantics would be. Actually, I still can't imagine that. I thought the whole idea of this feature was that it allowed you to take code that was executing outside of the class body and put it inside of the class body with basically identical semantics but now with visibility of private fields. So what semantics other than "exactly what the same code would do immediately outside of the class body" could there be? Why would we want to make it so that that only worked if the code for initialization happens to be synchronous? |
This is already a Stage 1 proposal about async class initialization. That proposal could have ramifications on the design of how something like
Its not quite that simple. As I've said before, a Rather than thinking of I'm not saying I'm opposed to ever adding support for You can still achieve async initialization without // promise to await in the module body
let cPromise;
// local binding so we don't eagerly export an uninitialized `C`.
let C_ = class C {
...
static {
// non-async init code
this.x = ...;
// async init code using async IIFE...
cPromise = (async () => {
await ...;
this.y = ...;
})();
}
}
// wait for the async init of `C` to complete before making the class available as an
// export (so circular references don't result in access to an uninitialized `C`)...
await cPromise;
// export the initialized `C` (exported binding is initialized only after `C`
// has completed its own initialization).
export let C = C_; |
That's a choice this proposal makes, not an inherent part of the design. Personally it seems like a surprising choice.
That doesn't really help me, since I think of static initializers in those languages as being blocks which are executed in the context of the class body.
Mm. To me it feels like leaving them out is adding complexity, not removing it. Now consumers have to know this extra fact about |
You've not commented on the part I wrote about how nowhere else in the language do we change |
Well, yes, but there's just not that many different contexts in the language at all. It doesn't seem like much of an inconsistency to me. (That said, I'd be happy for
Yup. |
Well, we could have had class Thing {
static {
console.log(1);
}
async static {
console.log(2);
}
static {
console.log(3);
}
}
// logs: 1, 3, 2 |
Does it support async-await?
The text was updated successfully, but these errors were encountered: