0
0
Node.jsframework~5 mins

Async/await syntax in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the async keyword do when placed before a function?
It makes the function return a Promise automatically and allows the use of await inside it to pause execution until a Promise resolves.
Click to reveal answer
beginner
How does the await keyword work inside an async function?
It pauses the function execution until the awaited Promise settles, then returns the resolved value or throws an error if rejected.
Click to reveal answer
intermediate
Why should you use try/catch blocks with async/await?
Because await can throw errors if the Promise rejects, so try/catch helps handle those errors gracefully.
Click to reveal answer
intermediate
What happens if you use await outside of an async function?
It causes a syntax error because await can only be used inside functions declared with async or in top-level modules supporting top-level await.
Click to reveal answer
advanced
How can you run multiple asynchronous operations in parallel using async/await?
Start all Promises without awaiting them immediately, then use await Promise.all([...]) to wait for all to finish together.
Click to reveal answer
What does the async keyword do to a function?
AMakes the function return a Promise
BMakes the function synchronous
CPrevents errors inside the function
DAutomatically logs function output
Where can you use the await keyword?
AAnywhere in the code
BOnly inside <code>async</code> functions or top-level modules
COnly inside <code>try/catch</code> blocks
DOnly in synchronous functions
What is the best way to handle errors when using await?
AUse <code>setTimeout</code>
BIgnore errors
CUse <code>console.log</code> only
DUse <code>try/catch</code> blocks
How do you run multiple async tasks in parallel with async/await?
AStart all Promises, then await <code>Promise.all</code>
BAwait each Promise one after another
CUse <code>setInterval</code>
DUse nested <code>async</code> functions
What happens if an awaited Promise rejects and you don't catch the error?
AThe function will ignore the error silently
BThe Promise will resolve anyway
CThe error will be unhandled and may crash the program
DThe program will pause indefinitely
Explain how async and await work together to handle asynchronous code in Node.js.
Think about how you wait for a friend to arrive before starting an activity.
You got /4 concepts.
    Describe how to handle errors when using await in your code.
    Imagine catching a ball so it doesn't fall and break.
    You got /3 concepts.