Skip to content

Day 2: Promises (then, catch, finally)

Handle asynchronous outcomes using Promises and chain transformations cleanly.

const fetchData = new Promise((resolve, reject) => {
const success = true;
if (success) resolve('Data retrieved successfully');
else reject(new Error('Network failure'));
});
fetchData
.then(data => console.log(data))
.catch(err => console.error(err.message))
.finally(() => console.log('Operation finished'));
  1. Write a function returning a Promise that resolves after a specified delay: sleep(ms).