Problem Statement
What is the output? Promise.resolve(1) .then(x => x + 1) .then(x => { throw new Error('error'); }) .catch(() => 1) .then(x => x + 1) .then(x => console.log(x));
Explanation
Promise chains execute in sequence. Let's trace through:
Start with Promise.resolve(1), value is 1.
First then: x plus 1 equals 2.
Second then: throws error, skips to catch.
Catch: returns 1, promise resolves with 1.
Next then: x plus 1 equals 2.
Final then: logs 2.
The catch handles the error and returns a value, so the chain continues normally.
After a catch, subsequent then blocks execute unless catch throws again.
This demonstrates error recovery in promise chains.
Code Solution
SolutionRead Only
// Tracing the promise chain
Promise.resolve(1) // 1
.then(x => x + 1) // 2
.then(x => { throw new Error(); }) // Error thrown
.catch(() => 1) // Caught, returns 1
.then(x => x + 1) // 2
.then(x => console.log(x)); // Logs: 2
// ERROR HANDLING IN CHAINS
// Error skips to next catch
Promise.resolve(1)
.then(x => { throw 'error'; })
.then(x => console.log('Skipped')) // Skipped
.catch(err => console.log('Caught')) // Caught
.then(() => console.log('Continues')); // Continues
// Catch returns value, chain continues
Promise.reject('error')
.catch(err => 'recovered')
.then(val => console.log(val)); // 'recovered'
// Catch throws, goes to next catch
Promise.reject('error1')
.catch(err => { throw 'error2'; })
.catch(err => console.log(err)); // 'error2'
// Multiple catches
Promise.reject('error')
.catch(err => {
console.log('First catch');
throw err; // Rethrow
})
.catch(err => {
console.log('Second catch');
});
// PROMISE METHODS
// Promise.all - all must succeed
Promise.all([Promise.resolve(1), Promise.resolve(2)])
.then(results => console.log(results)); // [1, 2]
Promise.all([Promise.resolve(1), Promise.reject('error')])
.catch(err => console.log(err)); // 'error'
// Promise.race - first to finish
Promise.race([
new Promise(resolve => setTimeout(() => resolve(1), 100)),
new Promise(resolve => setTimeout(() => resolve(2), 50))
]).then(result => console.log(result)); // 2
// Promise.allSettled - wait for all
Promise.allSettled([
Promise.resolve(1),
Promise.reject('error')
]).then(results => console.log(results));
// [{status: 'fulfilled', value: 1},
// {status: 'rejected', reason: 'error'}]
// Promise.any - first fulfilled
Promise.any([
Promise.reject('error1'),
Promise.resolve(2),
Promise.resolve(3)
]).then(result => console.log(result)); // 2
// CHAINING PATTERNS
// Return promise from then
fetch('/api/user')
.then(response => response.json()) // Returns promise
.then(user => fetch(`/api/posts/${user.id}`))
.then(response => response.json())
.then(posts => console.log(posts));
// Return value from then
Promise.resolve(5)
.then(x => x * 2) // Returns value, wrapped in promise
.then(x => x + 1)
.then(x => console.log(x)); // 11
// Finally block (runs regardless)
Promise.resolve(1)
.then(x => x + 1)
.catch(err => console.log(err))
.finally(() => console.log('Cleanup')); // Always runs