Created
March 12, 2019 09:42
-
-
Save matthewmueller/cc7fc6f10825c560a6539f035a053634 to your computer and use it in GitHub Desktop.
async constructors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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