Async function syntax in Javascript - Time & Space Complexity
We want to understand how the time it takes to run an async function changes as the input grows.
How does waiting for asynchronous tasks affect the total time?
Analyze the time complexity of the following code snippet.
async function fetchData(urls) {
const results = [];
for (const url of urls) {
const response = await fetch(url);
const data = await response.json();
results.push(data);
}
return results;
}
This function fetches data from a list of URLs one by one, waiting for each to finish before starting the next.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each URL and wait for its fetch to complete.
- How many times: Once for each URL in the input list.
Each URL causes a fetch that takes some time. Since the function waits for each fetch before starting the next, the total time adds up.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 fetches, done one after another |
| 100 | 100 fetches, each waited for in order |
| 1000 | 1000 fetches, all waited for sequentially |
Pattern observation: The total time grows roughly in direct proportion to the number of URLs.
Time Complexity: O(n)
This means the total time grows linearly with the number of URLs because each fetch waits for the previous one to finish.
[X] Wrong: "Async functions always run all tasks at the same time, so time does not grow with input size."
[OK] Correct: In this code, each fetch waits for the previous one to finish, so tasks run one after another, making total time grow with input size.
Understanding how async functions handle waiting helps you explain performance in real apps, showing you know how code runs over time.
What if we changed the code to start all fetches at once and then wait for all to finish? How would the time complexity change?
Practice
async keyword do when placed before a function in JavaScript?Solution
Step 1: Understand the role of
Theasyncasynckeyword marks a function to always return a promise.Step 2: Recognize the effect on
Inside anawaitasyncfunction, you can useawaitto pause until a promise resolves.Final Answer:
It makes the function return a promise and allows use ofawaitinside it. -> Option DQuick Check:
async= returns promise + allowsawait[OK]
- Thinking async makes function run faster
- Confusing async with synchronous blocking
- Believing async retries failed calls automatically
fetchData?Solution
Step 1: Recall async function declaration syntax
The correct syntax placesasyncbefore thefunctionkeyword.Step 2: Check each option's order
Only async function fetchData() { } correctly writesasync function fetchData() { }.Final Answer:
async function fetchData() { } -> Option AQuick Check:
async before function keyword = correct syntax [OK]
- Placing async after function name
- Writing async inside parentheses
- Mixing order of async and function keywords
async function greet() {
return 'Hello';
}
greet().then(console.log);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
Thethenmethod receives the resolved value 'Hello' and logs it.Final Answer:
Hello -> Option BQuick Check:
Async function returns promise, .then logs resolved value [OK]
- Expecting direct string without .then
- Confusing promise object with resolved value
- Thinking async function returns undefined
async function load() {
const data = await fetch('url');
return data.json;
}Solution
Step 1: Check usage of await and fetch
Usingawait fetch('url')is correct inside async function.Step 2: Inspect return statement
data.jsonis a function and needs parentheses:data.json().Final Answer:
Missing parentheses after json to call the method -> Option CQuick Check:
Call json() method with parentheses [OK]
- Forgetting parentheses on json method
- Thinking await can't be used inside async
- Misplacing async keyword
processData that fetches JSON from a URL and returns the value of the name property. Which code correctly does this?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 usesasyncand awaits bothfetchandresponse.json().Step 2: Check return of
async function processData(url) { const response = await fetch(url); const data = await response.json(); return data.name; } returnsnamepropertydata.nameafter 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 AQuick Check:
Await fetch and json(), then return property [OK]
- Not awaiting response.json()
- Missing async keyword
- Returning json method without calling it
Ready to prove it?
Pass the test and this pattern moves unknown
