I want to make an API like this:
class jsonReader {
public async load()
{
// some code
}
}
let reader = new jsonReader();
function foo(){
await reader.load();
// [ts] 'await' expression is only allowed within an async function.
}
How can I use Async/Await inside a synchronous function call?
You either make that function asynchronous as well and use await or use the promise from the returned function. All functions marked with async return Promise<T>. In your shared code the return type is Promise<void>, you can chain then to it.
function foo(){
reader.load().then(() => /*your code here*/);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With