Skip to content

Instantly share code, notes, and snippets.

@matthewmueller
Created March 12, 2019 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewmueller/cc7fc6f10825c560a6539f035a053634 to your computer and use it in GitHub Desktop.
Save matthewmueller/cc7fc6f10825c560a6539f035a053634 to your computer and use it in GitHub Desktop.
async constructors
class Car {
constructor() {
return Promise.resolve(this.new())
}
async new() {
await this.sleep(1000)
this._wheels = 4
return this
}
wheels() {
return this._wheels || 2
}
sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
}
;(async () => {
const car = await new Car()
console.log(car.wheels())
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment