Computer science fundamentals · Lesson 44 of 60 · Concept lesson · Optional
Asynchronous programming
Asynchronous code starts slow work, such as a network request, and carries on without waiting for it.
A callback runs when the work finishes, a promise stands for a result that will arrive, and async/await lets you write the waiting as if it were normal code.
Waiting without blocking keeps a program responsive.
Picture it
Code example
async function job() {
await Promise.resolve();
console.log("job finished");
}
async function main() {
const task = job();
console.log("started the job");
await task;
console.log("all done");
}
main();Output:
started the job job finished all done
main starts the job, prints, and only then waits for the job to finish.
import asyncio
async def job():
await asyncio.sleep(0)
print("job finished")
async def main():
task = asyncio.create_task(job())
print("started the job")
await task
print("all done")
asyncio.run(main())Output:
started the job job finished all done
main starts the job, prints, and only then waits for the job to finish.
Check yourself
Pick an answer for each question. You can retry as many times as you like.
Loading the questions…