Promises can be chained using the then()
method, allowing you to perform multiple asynchronous operations sequentially.
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Data fetched successfully!');
}, 2000);
});
};
fetchData()
.then((data) => {
console.log(data); // Output: "Data fetched successfully!"
return 'Processed data';
})
.then((processedData) => {
console.log(processedData); // Output: "Processed data"
})
.catch((error) => {
console.error(error);
});
© www.thecoderjob.com. All Rights Reserved. Designed by HTML Codex