Asynchronous Programming
Asynchronous programming lets a program start a slow task, such as a network request, file read or database query, and carry on with other work instead of waiting for it to finish. When the task completes, the program is notified and continues with the result. Synchronous code, by contrast, runs one step at a time and blocks while waiting.
In JavaScript, asynchronous work is handled with callbacks, Promises and the async/await syntax, which lets asynchronous code read like ordinary step-by-step code. Python (asyncio), C# and Kotlin (coroutines) offer similar features. It keeps user interfaces responsive and lets servers handle many requests at once.
async function loadUser(id) {
try {
const response = await fetch(`/api/users/${id}`);
const user = await response.json();
console.log(user.name);
} catch (error) {
console.error("Request failed", error);
}
}
loadUser(42);
console.log("This runs before the user is loaded");