Introduction
Promise chaining helps run multiple tasks one after another without confusing code. It makes handling steps that depend on each other easy and clear.
Jump into concepts and practice - no test required
function task1() {
return new Promise((resolve, reject) => {
// do something
const result1 = 'result1';
resolve(result1);
});
}
function task2(previousResult) {
return new Promise((resolve, reject) => {
// use previousResult
const result2 = 'result2';
resolve(result2);
});
}
task1()
.then(result1 => task2(result1))
.then(result2 => {
// use result2
})
.catch(error => {
// handle any error from task1 or task2
});Promise.resolve(5) .then(value => value * 2) .then(value => console.log(value));
Promise.resolve()
.then(() => {
throw new Error('Oops');
})
.catch(error => console.log(error.message));Promise.resolve(1) .then(value => Promise.resolve(value + 1)) .then(value => Promise.resolve(value + 1)) .then(value => console.log(value));
Promise.resolve(10) .then(value => value + 5) .then(value => { throw new Error('Fail'); }) .then(value => console.log('Won\'t run')) .catch(error => console.log('Error caught:', error.message));
function fetchNumber() {
return new Promise((resolve) => {
setTimeout(() => {
resolve(10);
}, 500);
});
}
function addFive(number) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(number + 5);
}, 500);
});
}
function multiplyByTwo(number) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(number * 2);
}, 500);
});
}
fetchNumber()
.then(result => {
console.log('Fetched number:', result);
return addFive(result);
})
.then(result => {
console.log('After adding five:', result);
return multiplyByTwo(result);
})
.then(result => {
console.log('After multiplying by two:', result);
})
.catch(error => {
console.error('Error:', error);
});promise chaining in Node.js?.then() ensures order, not parallel or immediate stop.promise1 and promise2?.then().then() method expects a function to call when the promise resolves.promise2(). Options A and B call promise2() immediately or pass wrong types. promise1.then.then(promise2) has invalid chaining syntax.Promise.resolve(5)
.then(x => x + 1)
.then(x => { throw new Error('Fail'); })
.catch(err => 'Caught: ' + err.message)
.then(x => console.log(x));then adds 1 to 5, resulting in 6. The second then throws an error.catch catches the error and returns the string 'Caught: Fail'. The last then logs this string.fetchData() .then(data => processData(data)) .then(result => console.log(result)) .catch(console.error())
console.error() calls the function immediately, passing its result (undefined) instead..catch(console.error) without parentheses to pass the function itself.function step1() { return Promise.resolve(2); }
function step2(x) { return Promise.resolve(x * 3); }
function step3(x) { return x + 4; }step1() .then(x => step2(x)) .then(x => step3(x)) .then(console.log);
step1() resolves to 2. Then step2(2) resolves to 6 (2*3).step3(6) returns 10 (6+4) synchronously. Since then accepts a value or promise, it passes 10 to next then which logs 10.