As the question stated. Will I be allowed to do this:
class MyClass {
async constructor(){
return new Promise()
}
}
You can get a promise from the return value, and await on that:
class User {
constructor() {
this.promise = this._init()
}
async _init() {
const response = await fetch('https://jsonplaceholder.typicode.com/users')
const users = await response.json()
this.user = users[Math.floor(Math.random() * users.length)]
}
}
(async () {
const user = new User()
await user.promise
return user
})().then(u => {
$('#result').text(JSON.stringify(u.user, null, 2))
}).catch(err => {
console.error(err)
})