Is possible to call a function in typescript as if it were with async but without async? It’s useful for an ORM when we are fetching data with relations from the database. We have to add “await” every time when we call async function.
Currently we have to add await every time:
const post = await post()
const author = await post.author()
const address = await author.address()
Solution would be an interface like “AutoAwait” where every method will be called with await keyword
Example implementation:
class Post implements AutoAwait {
async function user() {
const user = await db.users.find(this.userId)
return user
}
}
class User implements AutoAwait {
async function address() {
const address = await db.address.find(this.addressId)
return address
}
}
Execution without await:
const address = post.author().address()
Is it possible in typescript? If it’s not possible maybe it’s a good feature to add to typescript? What do you think?
Have a nice day
Ryszard
Ryszard Romanik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.