async and await

asyncio gives you concurrency in a single thread, and it runs in the browser.

async def makes a coroutine function. Calling it does not run the body. It returns a coroutine object, a paused piece of work. Python warns "coroutine was never awaited" when you forget this, a common bug.

Inside another async def, await runs a coroutine and waits for its result. While it waits, for example on asyncio.sleep or a network reply, the event loop can run other tasks. That is the whole trick: tasks take turns at each await.

asyncio.run(main()) starts the event loop, runs main to the end, and returns its result. You call it once, at the top of the program.

Never call time.sleep inside async code. It blocks the whole loop, so nothing else runs. Use await asyncio.sleep(...).

Run it. Then remove the await before fetch_name(1) and see what name becomes.