Async functions let your code wait for things like data from the internet without stopping everything else. This makes your programs faster and smoother.
When you need to get data from a website or server.
When you want to wait for a timer or delay without freezing the page.
When you want to handle tasks that take time, like reading files or databases.
When you want your program to keep working while waiting for something to finish.
Syntax
Javascript
asyncfunction functionName(parameters) {
// code that can use await
}
Use the async keyword before function to make it asynchronous.
Inside an async function, you can use await to pause until a promise finishes.
Examples
A simple async function that returns a greeting.
Javascript
asyncfunction greet() {
return'Hello!';
}
This function waits 1 second before printing a message.
2. Which of the following is the correct syntax to declare an async function named fetchData?
easy
A. async function fetchData() { }
B. function async fetchData() { }
C. function fetchData async() { }
D. async fetchData function() { }
Solution
Step 1: Recall async function declaration syntax
The correct syntax places async before the function keyword.
Step 2: Check each option's order
Only async function fetchData() { } correctly writes async function fetchData() { }.
Final Answer:
async function fetchData() { } -> Option A
Quick Check:
async before function keyword = correct syntax [OK]
Hint: Put async before function keyword for correct syntax [OK]
Common Mistakes:
Placing async after function name
Writing async inside parentheses
Mixing order of async and function keywords
3. What will be the output of this code?
async function greet() {
return 'Hello';
}
greet().then(console.log);
medium
A. Promise { 'Hello' }
B. Hello
C. undefined
D. SyntaxError
Solution
Step 1: Understand async function return value
An async function always returns a promise that resolves to the returned value.
Step 2: Analyze the .then usage
The then method receives the resolved value 'Hello' and logs it.
Final Answer:
Hello -> Option B
Quick Check:
Async function returns promise, .then logs resolved value [OK]
Hint: Async returns promise; .then logs resolved value [OK]
Common Mistakes:
Expecting direct string without .then
Confusing promise object with resolved value
Thinking async function returns undefined
4. Identify the error in this async function:
async function load() {
const data = await fetch('url');
return data.json;
}
medium
A. fetch should not be awaited
B. Cannot use await inside async function
C. Missing parentheses after json to call the method
D. async keyword is misplaced
Solution
Step 1: Check usage of await and fetch
Using await fetch('url') is correct inside async function.
Step 2: Inspect return statement
data.json is a function and needs parentheses: data.json().
Final Answer:
Missing parentheses after json to call the method -> Option C
Quick Check:
Call json() method with parentheses [OK]
Hint: Remember to call json() with parentheses after await fetch [OK]
Common Mistakes:
Forgetting parentheses on json method
Thinking await can't be used inside async
Misplacing async keyword
5. You want to write an async function processData that fetches JSON from a URL and returns the value of the name property. Which code correctly does this?
hard
A. async function processData(url) {
const response = await fetch(url);
const data = await response.json();
return data.name;
}
B. async function processData(url) {
const response = fetch(url);
const data = response.json();
return data.name;
}
C. function processData(url) {
const response = await fetch(url);
const data = await response.json();
return data.name;
}
D. async function processData(url) {
const response = await fetch(url);
const data = response.json;
return data.name;
}
Solution
Step 1: Confirm async function and await usage
async function processData(url) {
const response = await fetch(url);
const data = await response.json();
return data.name;
} correctly uses async and awaits both fetch and response.json().
Step 2: Check return of name property
async function processData(url) {
const response = await fetch(url);
const data = await response.json();
return data.name;
} returns data.name after parsing JSON, which is correct.
Final Answer:
async function processData(url) {
const response = await fetch(url);
const data = await response.json();
return data.name;
} -> Option A
Quick Check:
Await fetch and json(), then return property [OK]
Hint: Await fetch and json(), then return needed property [OK]
Common Mistakes:
Not awaiting response.json()
Missing async keyword
Returning json method without calling it
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.