0
0
Javascriptprogramming~20 mins

Then and catch methods in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Promise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Promise chaining with then and catch
What is the output of this code snippet?
Javascript
Promise.resolve(5)
  .then(x => x * 2)
  .then(x => { throw new Error('Oops'); })
  .catch(e => 'Error caught')
  .then(x => x + ' done')
  .then(console.log);
AUncaught Error
BOops done
C10 done
DError caught done
Attempts:
2 left
💡 Hint
Remember catch handles errors and passes its return value to the next then.
Predict Output
intermediate
2:00remaining
Error propagation in promise chains
What will be logged by this code?
Javascript
Promise.reject('fail')
  .then(() => 'success')
  .catch(err => err)
  .then(console.log);
Asuccess
BUncaught Error
Cfail
Dundefined
Attempts:
2 left
💡 Hint
Catch returns the error string, which is passed to the next then.
Predict Output
advanced
2:00remaining
Multiple catch blocks in promise chain
What is the output of this code?
Javascript
Promise.resolve('start')
  .then(val => { throw 'error1'; })
  .catch(err => {
    console.log('Caught:', err);
    throw 'error2';
  })
  .catch(err => console.log('Caught again:', err));
ACaught: error1
BCaught: error1\nCaught again: error2
CCaught again: error2
DUncaught error1
Attempts:
2 left
💡 Hint
A catch can throw again to be caught by another catch.
🧠 Conceptual
advanced
2:00remaining
Promise then return values
Which option correctly describes what happens when a then callback returns a promise?
AThe next then waits for the returned promise to resolve before running.
BThe next then runs immediately ignoring the returned promise.
CThe returned promise is ignored and the next then receives undefined.
DThe chain breaks and no further thens run.
Attempts:
2 left
💡 Hint
Think about how promise chaining works with promises returned inside then.
Predict Output
expert
3:00remaining
Complex promise chain with nested then and catch
What is the final output logged by this code?
Javascript
Promise.resolve(1)
  .then(x => x + 1)
  .then(x => {
    return Promise.reject('fail at step 3');
  })
  .catch(err => {
    console.log('Caught:', err);
    return 10;
  })
  .then(x => x * 2)
  .then(x => { throw 'final error'; })
  .catch(err => console.log('Final catch:', err));
ACaught: fail at step 3\nFinal catch: final error
BFinal catch: final error
CCaught: fail at step 3
DUncaught final error
Attempts:
2 left
💡 Hint
Follow the chain carefully, noting where errors are caught and what values are returned.