Code that uses async
and await
is asynchronous,but it looks a lot like synchronous code.For example, here’s some code that uses await
to wait for the result of an asynchronous function:
To use await
, code must be in an async
function—afunction marked as async
:
Use try
, catch
, and finally
to handle errors and cleanup in code that usesawait
:
You can use await
multiple times in an async
function.For example, the following code waits three timesfor the results of functions:
If you get a compile-time error when using await
,make sure await
is in an async
function.For example, to use await
in your app’s main()
function,the body of main()
must be marked as :