Bird
Raised Fist0
Javascriptprogramming~5 mins

Async function syntax in Javascript - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Async function syntax
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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
1010 fetches, done one after another
100100 fetches, each waited for in order
10001000 fetches, all waited for sequentially

Pattern observation: The total time grows roughly in direct proportion to the number of URLs.

Final Time Complexity

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.

Common Mistake

[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.

Interview Connect

Understanding how async functions handle waiting helps you explain performance in real apps, showing you know how code runs over time.

Self-Check

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

(1/5)
1. What does the async keyword do when placed before a function in JavaScript?
easy
A. It converts the function into a synchronous blocking call.
B. It makes the function run faster by using multiple CPU cores.
C. It automatically retries the function if it fails.
D. It makes the function return a promise and allows use of await inside it.

Solution

  1. Step 1: Understand the role of async

    The async keyword marks a function to always return a promise.
  2. Step 2: Recognize the effect on await

    Inside an async function, you can use await to pause until a promise resolves.
  3. Final Answer:

    It makes the function return a promise and allows use of await inside it. -> Option D
  4. Quick Check:

    async = returns promise + allows await [OK]
Hint: Async means function returns promise and can use await inside [OK]
Common Mistakes:
  • Thinking async makes function run faster
  • Confusing async with synchronous blocking
  • Believing async retries failed calls automatically
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

  1. Step 1: Recall async function declaration syntax

    The correct syntax places async before the function keyword.
  2. Step 2: Check each option's order

    Only async function fetchData() { } correctly writes async function fetchData() { }.
  3. Final Answer:

    async function fetchData() { } -> Option A
  4. 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

  1. Step 1: Understand async function return value

    An async function always returns a promise that resolves to the returned value.
  2. Step 2: Analyze the .then usage

    The then method receives the resolved value 'Hello' and logs it.
  3. Final Answer:

    Hello -> Option B
  4. 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

  1. Step 1: Check usage of await and fetch

    Using await fetch('url') is correct inside async function.
  2. Step 2: Inspect return statement

    data.json is a function and needs parentheses: data.json().
  3. Final Answer:

    Missing parentheses after json to call the method -> Option C
  4. 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

  1. 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().
  2. 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.
  3. Final Answer:

    async function processData(url) { const response = await fetch(url); const data = await response.json(); return data.name; } -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard