- Definition: Synchronous programming executes code line by line, meaning each line of code waits for the previous one to finish before executing. This can lead to delays if a particular task takes time (e.g., reading a file or making a network request).
- Example:
Output:
console.log('Start'); console.log('Middle'); console.log('End');
In this case, the execution is in order.Start Middle End
- Definition: Asynchronous programming allows multiple tasks to run concurrently without waiting for the previous task to complete. This is especially useful for tasks that take time, such as fetching data from an API or performing file operations.
- Example:
Output:
console.log('Start'); setTimeout(() => { console.log('Middle'); }, 1000); console.log('End');
TheStart End Middle
setTimeout
function runs asynchronously, allowing the code to continue executing while it waits.
- Definition: A promise is an object representing the eventual completion (or failure) of an asynchronous operation and its resulting value.
- States of a Promise:
- Pending: Initial state, neither fulfilled nor rejected.
- Fulfilled: Operation completed successfully.
- Rejected: Operation failed.
let promise = new Promise((resolve, reject) => {
let success = true; // Simulating a condition
if (success) {
resolve('Task completed successfully!');
} else {
reject('Task failed.');
}
});
- Definition:
.then()
is used to handle the successful completion of a promise. It accepts a callback function that gets executed when the promise is resolved. - Example:
promise.then((message) => { console.log(message); // Output: Task completed successfully! });
- Definition:
.catch()
is used to handle errors or rejections in a promise. It accepts a callback function that gets executed when the promise is rejected. - Example:
promise.catch((error) => { console.log(error); // Output: Task failed. });
Definition: The fetch()
method is used to make network requests and returns a promise that resolves to the response object. This can be used to make API calls to retrieve or send data.
fetch(url)
.then(response => response.json()) // Converting the response to JSON
.then(data => console.log(data)) // Handling the data
.catch(error => console.error('Error:', error)); // Handling errors
- Example:
fetch('https://api.example.com/data') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.log('Error:', error));
Definition: async/await
provides a way to work with asynchronous code in a more synchronous-looking manner, making the code easier to read and understand.
- async: Declares a function as asynchronous, meaning it will return a promise.
- await: Pauses the execution of an async function until the promise is resolved or rejected.
async function fetchData() {
try {
let response = await fetch('https://api.example.com/data');
let data = await response.json();
console.log(data);
} catch (error) {
console.log('Error:', error);
}
}
fetchData();
Explanation:
fetchData()
is an asynchronous function that usesawait
to wait for thefetch
call to complete.- The
try
block is used to handle successful data retrieval, and thecatch
block catches any errors that occur during the process.
- Use
async/await
when you need to handle multiple asynchronous operations sequentially. - It makes the code more readable, especially when dealing with complex chains of
.then()
and.catch()
.