Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result.
Many functions provided by browsers, especially the most interesting ones, can potentially take a long time, and therefore, are asynchronous. For example:
- Making HTTP requests using
fetch() - Accessing a user's camera or microphone using
getUserMedia() - Asking a user to select files using
showOpenFilePicker()
This behavior is commonly used in web development to handle tasks that might take time to complete, such as fetching data from a server, reading files, or executing time-consuming processes.
Asynchronous operations in JavaScript are typically managed using callbacks, promises, or more recently, async/await syntax.
-
Callbacks: Functions passed as arguments to other functions, executed after the completion of a particular task. This approach can lead to callback hell, where nested callbacks make code hard to read and maintain.
-
Promises: Introduced to handle asynchronous operations in a more organized way, promises represent a value that may not be available yet but will be resolved in the future, either successfully with a value or unsuccessfully with a reason for failure.
-
Async/Await: It is a more modern and readable way to write asynchronous code. It allows developers to write asynchronous code that looks and behaves more like synchronous code, making it easier to understand and maintain.
Example:
Here's a basic illustration using async/await with a promise:
async function fetchData() {
try {
let response = await fetch("https://api.example.com/data");
let data = await response.json();
return data;
} catch (error) {
console.error("Error fetching data:", error);
throw error;
}
}
fetchData()
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});In this example function fetchData() uses async/await to fetch data from an API asynchronously. The await keyword is used to pause the execution until the promise returned by fetch resolves or rejects. The try/catch block is used to handle any potential errors during the asynchronous operations.