What is the correct usage of the async keyword in typescript? My understanding is that it is to only allow the usage of await in a function body. Specifically,
async returnPromise() : Promise<any> {}
export const interfaceFunction() = async () {
return returnPromise();
}
// Usage in another module
const returnValue = await interfaceFunction();
Is the interfaceFunction explicitly required to be declared as async in this case? The code works with or without the async keyword (and the return type remains a Promise in both the cases as well).
Promise. You don't need to declareasyncif your function return aPromise. You can directlyawaitaPromisejust likeawaitanasyncfunction. If you declare your functionasync, you shouldn't return aPromise, otherwise you have toawaittwice. You shouldreturn await returnPromise();if you really need to use aPromisefrom other source.