I\'m adapting a library that uses callback to use Promises. It\'s working when I use then()
, but it doesn\'t work when I use await
.
SyntaxError: await is only valid in async function
- just like the error tells you, you may only use await
inside a function which is marked as async
. So you cannot use the await
keyword anywhere else.
https://basarat.gitbooks.io/typescript/docs/async-await.html
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-7.html
examples:
function test() {
await myOtherFunction() // NOT working
}
async function test() {
await myOtherFunction() //working
}
You can also make anonymous callback functions async
:
myMethod().then(async () => {
await myAsyncCall()
})