What can ES6 Classes provide, as a pattern of organization, to asynchronous code. Below is an example with ES7 async/await, can an ES6-class have an asynchronous method, or constructor in ES7?
Can I do:
class Foo { async constructor() { let res = await getHTML(); this.res = res } }
And, if not how should a constructor work that does this?
class Foo { constructor() { getHTML().then( function (res) { this.res = res } } }
If neither of these patterns work, can a constructor (and moreover classes) in an ES6 class
support any form of asynchronicity that operates on the object's state? Or, are they only for purely synchronous code bases? The above examples are in the constructor, but they don't need to be.. Pushing the problem down one more level..
class Foo { myMethod () { /* Can I do anything async here */ } }
Or, with a getter...
class Foo { get myProp() { /* Is there any case that this is usefully asynchronous */ } }
The only examples I could think of is to run something in parallel inside of the same method/constructor/getter, but to have the whole thing resolve before conclusion. I'm just confused because it seems with all the push to fully asynchronous libraries, this just serves to confuse things. Except for textbook examples, I can't find one application they're useful for.
ES6 makes asynchronous programming easier with the async and await keywords.
JavaScript functions are not asynchronous. Some very limited set of functions have an asynchronous API: addEventListener , setTimeout , setInterval .
There are two types of Class in ES6: parent class/super class: The class extended to create new class are know as a parent class or super class. child/sub classes: The class are newly created are known as child or sub class. Sub class inherit all the properties from parent class except constructor.
async/await is not part of ES6, so it won't run in environments that (only) support ES6. You need to convert it do ES6 code first (e.g. using Babel or regenerator).
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.
Another way that Classes can be useful for arranging asynchronous tasks is with the exclusive use of static methods.
class Organizer { static async foo() { const data = await this.bar(); data.key = value; return data; } static async bar() { return {foo:1, bar:2} } }; Organizer.foo();
Of course, this is no different than creating a simple object literal, or a new file and including it, except you can more cleanly extend
it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With