Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
A Promise is an object representing the eventual completion or failure of an async operation. It's like a receipt - you get it immediately, but the result comes later!
Initial state - operation hasn't completed yet
Operation completed successfully with a result
Operation failed with an error
Use new Promise() with resolve and reject
promise.then(result => /* use result */)Runs when promise is fulfilled
promise.catch(error => /* handle error */)Runs when promise is rejected
promise.finally(() => /* cleanup */)Runs regardless of success/failure
Complete promise handling
Return a value or promise from then() to chain operations
fetchUser()
.then(user => {
console.log('Got user:', user);
return fetchPosts(user.id); // Return new promise
})
.then(posts => {
console.log('Got posts:', posts);
return fetchComments(posts[0].id);
})
.then(comments => {
console.log('Got comments:', comments);
})
.catch(error => {
console.log('Any error:', error);
});Sequential async operations
Promise.all() waits for ALL promises to succeed. If ANY fails, the whole thing fails.
const promise1 = Promise.resolve(10);
const promise2 = Promise.resolve(20);
const promise3 = Promise.resolve(30);
Promise.all([promise1, promise2, promise3])
.then(results => {
console.log(results); // [10, 20, 30]
});
// If any fails, all fails
const p1 = Promise.resolve('Success 1');
const p2 = Promise.reject('Failed!');
const p3 = Promise.resolve('Success 3');
Promise.all([p1, p2, p3])
.catch(error => {
console.log(error); // 'Failed!'
});Parallel operations
Returns as soon as first promise settles (success or fail)
Waits for ALL, returns results even if some failed
Returns when first promise succeeds, ignores failures
race(), allSettled(), any()