What problem do async and await solve in JavaScript?
They help manage asynchronous code in a way that looks and behaves like synchronous code, making it easier to read and write.
Click to reveal answer
beginner
What does the async keyword do when added before a function?
It makes the function return a Promise and allows the use of await inside it.
Click to reveal answer
beginner
How does await improve code readability?
It pauses the function execution until the Promise resolves, so you can write code that looks like normal sequential steps.
Click to reveal answer
intermediate
Why is using callbacks sometimes problematic compared to async/await?
Callbacks can lead to nested code that is hard to read and maintain, often called 'callback hell'. async/await avoids this by flattening the code structure.
Click to reveal answer
intermediate
What happens if you forget to use await before a Promise inside an async function?
The function continues without waiting for the Promise to resolve, which can cause unexpected behavior or errors.
Click to reveal answer
What does the async keyword do to a function?
AMakes the function return a Promise
BMakes the function synchronous
CBlocks the entire program until it finishes
DAutomatically runs the function in the background
✗ Incorrect
The async keyword makes the function always return a Promise, allowing use of await inside it.
What is the main benefit of using await inside an async function?
AIt pauses execution until the Promise resolves
BIt makes the function run faster
CIt converts synchronous code to asynchronous
DIt prevents errors automatically
✗ Incorrect
await pauses the function execution until the Promise resolves, making asynchronous code easier to read.
Which problem does async/await help avoid compared to callbacks?
AMemory leaks
BSyntax errors in JavaScript
CSlow internet connections
DCallback hell with deeply nested code
✗ Incorrect
async/await helps avoid callback hell by flattening asynchronous code structure.
If you forget to use await before a Promise, what happens?
AThe Promise resolves instantly
BThe function continues without waiting for the Promise
CThe program crashes immediately
DThe function becomes synchronous
✗ Incorrect
Without await, the function does not wait for the Promise, which can cause unexpected results.
Why is asynchronous programming important in JavaScript?
ATo use more memory
BTo make the code run slower
CTo avoid blocking the user interface during long tasks
DTo write less code
✗ Incorrect
Asynchronous programming prevents blocking the UI, keeping apps responsive during long operations.
Explain why async and await are useful in JavaScript programming.
Think about how they help with waiting for tasks without freezing the program.
You got /4 concepts.
Describe what happens if you use an async function but forget to use await when calling a Promise inside it.
Consider how the function flow changes without waiting.
You got /4 concepts.
Practice
(1/5)
1. Why do we use async and await in JavaScript?
easy
A. To make the code run faster by skipping steps
B. To handle slow tasks without freezing the program
C. To write code that only works in old browsers
D. To stop the program completely until a task finishes
Solution
Step 1: Understand the problem with slow tasks
Slow tasks like fetching data can freeze the program if handled synchronously.
Step 2: Role of async and await
Async and await let the program wait for slow tasks without freezing, keeping it smooth.
Final Answer:
To handle slow tasks without freezing the program -> Option B
Quick Check:
Async/await = smooth waiting [OK]
Hint: Async/await = smooth waiting without freezing [OK]
Common Mistakes:
Thinking async/await makes code faster
Believing async/await stops the whole program
Confusing async/await with old browser code
2. Which of the following is the correct way to declare an async function in JavaScript?
easy
A. function async fetchData() {}
B. function fetchData() async {}
C. async function fetchData() {}
D. function fetchData async() {}
Solution
Step 1: Recall async function syntax
The correct syntax places async before the function keyword.
Step 2: Check each option
Only async function fetchData() {} correctly uses async function fetchData() {}.
Final Answer:
async function fetchData() {} -> Option C
Quick Check:
Async before function keyword [OK]
Hint: Async keyword goes before function name [OK]
Common Mistakes:
Placing async after function name
Adding async inside function parentheses
Writing async after function body
3. What will be the output of this code?
async function greet() {
return 'Hello';
}
greet().then(console.log);
medium
A. 'Hello'
B. Promise { 'Hello' }
C. undefined
D. Error: greet is not a function
Solution
Step 1: Understand async function return
An async function returns a Promise that resolves to the returned value.
Step 2: Using .then to get value
The .then(console.log) prints the resolved value 'Hello'.
Final Answer:
'Hello' -> Option A
Quick Check:
Async function returns Promise, .then logs value [OK]
Hint: Async returns Promise; .then gets resolved value [OK]
Common Mistakes:
Expecting direct string without .then
Confusing Promise object with string
Thinking async function returns undefined
4. Find the error in this code:
async function fetchData() {
const data = await fetch('https://api.example.com');
return data.json();
}
const result = fetchData();
console.log(result);
medium
A. data.json() is not a function
B. fetchData() should not be async
C. Cannot use await inside async function
D. Missing await before fetchData() call
Solution
Step 1: Check fetchData() call
fetchData() returns a Promise, so logging it directly shows a Promise object.
Step 2: Fix by awaiting fetchData()
Adding await before fetchData() inside an async context gets the resolved data.
Final Answer:
Missing await before fetchData() call -> Option D
Quick Check:
Await needed to get Promise result [OK]
Hint: Await async calls to get actual result [OK]
Common Mistakes:
Logging Promise instead of awaited value
Removing async keyword incorrectly
Misunderstanding data.json() usage
5. You want to fetch user data and then fetch posts by that user. Which code correctly uses async/await to do this step-by-step without freezing the program?
function getUser() {
return fetch('/user').then(res => res.json());
}
function getPosts(userId) {
return fetch(`/posts?user=${userId}`).then(res => res.json());
}
// Fill in the blanks
async function loadData() {
const user = _____ getUser();
const posts = _____ getPosts(user.id);
console.log(posts);
}
hard
A. await, await
B. await, then
C. then, await
D. then, then
Solution
Step 1: Understand async function usage
Inside async functions, use await to wait for Promises step-by-step.
Step 2: Apply await to both calls
Both getUser() and getPosts() return Promises, so both need await.
Final Answer:
await, await -> Option A
Quick Check:
Use await for sequential async calls [OK]
Hint: Use await for each async call inside async function [OK]
Common Mistakes:
Mixing then and await inside async function
Forgetting await causing Promise logs
Using then without returning inside async
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.