Day 2: Promises (then, catch, finally)
Daily Objective
Section titled “Daily Objective”Handle asynchronous outcomes using Promises and chain transformations cleanly.
1. Promises
Section titled “1. Promises”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'));Daily Hands-on Practice (70%)
Section titled “Daily Hands-on Practice (70%)”- Write a function returning a Promise that resolves after a specified delay:
sleep(ms).