0
0
Node.jsframework~20 mins

Promises for cleaner async in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Promise chain?
Consider the following Node.js code using Promises. What will be logged to the console?
Node.js
Promise.resolve(5)
  .then(x => x * 2)
  .then(x => { throw new Error('Fail'); })
  .catch(() => 10)
  .then(x => x + 1)
  .then(console.log);
A11
BError: Fail
C6
Dundefined
Attempts:
2 left
💡 Hint
Remember that catch returns a resolved Promise with the value it returns.
component_behavior
intermediate
2:00remaining
Which option correctly returns a Promise that resolves after 1 second?
You want to create a function that returns a Promise resolving with 'done' after 1 second. Which code does this correctly?
Afunction wait() { return Promise.resolve(setTimeout(() => 'done', 1000)); }
Bfunction wait() { setTimeout(() => Promise.resolve('done'), 1000); }
Cfunction wait() { return new Promise(resolve => setTimeout(() => resolve('done'), 1000)); }
Dfunction wait() { return new Promise(resolve => resolve(setTimeout(() => 'done', 1000))); }
Attempts:
2 left
💡 Hint
Remember that setTimeout returns a timer ID, not a Promise.
🔧 Debug
advanced
2:00remaining
Why does this Promise chain never log anything?
Look at this code snippet. Why does it never log 'Finished'?
Node.js
Promise.resolve()
  .then(() => { throw 'Error happened'; })
  .then(() => console.log('Finished'));
ABecause the Promise never resolves.
BBecause console.log is inside a then that never runs due to syntax error.
CBecause the Promise chain is missing a return statement.
DBecause the error is thrown but not caught, so the last then is skipped.
Attempts:
2 left
💡 Hint
Think about what happens when a Promise rejects and no catch is present.
📝 Syntax
advanced
2:00remaining
Which option correctly chains Promises to fetch data and process it?
Given fetchData() returns a Promise resolving to data, which code correctly fetches and logs the processed data?
Node.js
function process(data) { return data.length; }
AfetchData().then(data => process(data)).then(result => console.log(result));
BfetchData().then(process).then(console.log);
CfetchData().then(process()).then(console.log);
DfetchData().then(data => { process(data); }).then(console.log);
Attempts:
2 left
💡 Hint
Remember to pass functions, not call them immediately in then.
🧠 Conceptual
expert
2:00remaining
What is the behavior of Promise.all with mixed resolved and rejected Promises?
Given these Promises: p1 resolves, p2 rejects, p3 resolves. What does Promise.all([p1, p2, p3]) do?
AIt resolves with an array of all results including the rejection reason.
BIt immediately rejects with p2's rejection reason, ignoring p1 and p3 results.
CIt waits for all Promises and then resolves with only the successful results.
DIt resolves with the first resolved Promise's value.
Attempts:
2 left
💡 Hint
Promise.all rejects as soon as any Promise rejects.