0
0
Node.jsframework~20 mins

Promise chaining in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Promise Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Promise chain?
Consider the following Node.js code using Promise chaining. What will be logged to the console?
Node.js
Promise.resolve(5)
  .then(x => x * 2)
  .then(x => { console.log(x); return x + 1; })
  .then(x => console.log(x));
A11
B5\n6
C10
D10\n11
Attempts:
2 left
💡 Hint
Remember each then returns a new Promise with the returned value.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in Promise chaining?
Identify which code snippet will cause a syntax error when chaining Promises in Node.js.
APromise.resolve(1).then(x => { return x + 1 }).then(x => console.log(x));
BPromise.resolve(1).then(x => x + 1).then(x => console.log(x))
CPromise.resolve(1).then(x => x + 1).then(x => { console.log(x) });
DPromise.resolve(1).then(x => x + 1).then(x => console.log(x));
Attempts:
2 left
💡 Hint
Look carefully at parentheses and braces.
🔧 Debug
advanced
2:00remaining
Why does this Promise chain not log the expected value?
Given the code below, why does the console.log output 'undefined' instead of the expected number?
Node.js
Promise.resolve(3)
  .then(x => { x * 3; })
  .then(x => console.log(x));
AThe first then does not return a value, so x is undefined in the next then.
BPromise.resolve(3) is rejected, so then is skipped.
Cconsole.log is called before the Promise resolves.
DThe second then has a syntax error causing undefined output.
Attempts:
2 left
💡 Hint
Check if the first then returns a value explicitly.
state_output
advanced
2:00remaining
What is the final value of 'result' after this Promise chain?
Analyze the code and determine the value of the variable 'result' after the Promise chain completes.
Node.js
let result = 0;
Promise.resolve(2)
  .then(x => x + 3)
  .then(x => { result = x * 2; })
  .then(() => console.log('Done'));
A10
B0
CNaN
D5
Attempts:
2 left
💡 Hint
Follow the values through each then and how 'result' is assigned.
🧠 Conceptual
expert
3:00remaining
Which option correctly handles errors in a Promise chain?
Choose the code snippet that properly catches errors occurring in any step of the Promise chain.
A
Promise.resolve()
  .then(() => { throw new Error('Fail'); })
  .then(null, err => console.log('Caught:', err.message));
B
Promise.resolve()
  .then(() => { throw new Error('Fail'); })
  .catch(err => console.log('Caught:', err.message));
C
Promise.resolve()
  .then(() => { throw new Error('Fail'); })
  .then(() => console.log('No error'))
  .catch(err => console.log('Caught:', err.message));
D
Promise.resolve()
  .catch(err => console.log('Caught:', err.message))
  .then(() => { throw new Error('Fail'); });
Attempts:
2 left
💡 Hint
Errors thrown in any then should be caught by catch at the end.