asyncio call async function code example
Example 1: python async await
import asyncio
async def print_B():
print("B")
async def main_def():
print("A")
await asyncio.gather(print_B())
print("C")
asyncio.run(main_def())
Example 2: how to make an async function
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
//async function:
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
// expected output: 'resolved'
}
asyncCall();