Are JavaScript ES6 Classes of any use with asynchronous code bases?

Can I do async constructor()

No, that’s a syntax error – just like constructor* (). A constructor is a method that doesn’t return anything (no promise, no generator), it only initialises the instance.

And, if not how should a constructor work that does this

Such a constructor should not exist at all, see Is it bad practice to have a constructor function return a Promise?

Can ES6 classes support any form of asynchrony that operates on the object’s state? Or, are they only for purely synchronous code bases?

Yes, you can use asynchronous methods (even with the proposed async syntax) on classes, and getters can return promises as well.

However, you will need to decide what should happen when a method is called while some asynchronous process is still active. If you want it to sequence all your operations, you should store your instance’s state inside a promise for the end of that sequence that you can chain onto. Or, if you want to allow parallel operations, the best approach is to make your instances immutable and return a promise for another instance.

Leave a Comment