Skip to main content
FREE Mock interview & resume review with Sumit. Free right now, no payment needed.Book a slot

How Web APIs Handle Asynchronous Operations

Topic 3JavaScript Part 2 (Async JS)

How Web APIs Handle Asynchronous Operations

Introduction

Web APIs play a crucial role in enabling JavaScript to perform asynchronous tasks. They interact with the call stack and event loop to handle operations like timers, HTTP requests, and DOM events.

Step-by-Step Explanation

Execution Flow:

  • •Call Stack: Logs 'Start' and 'End'.
  • •Web API: setTimeout sends the callback to the browser's Web API with a timer of 2 seconds.
  • •Callback Queue: After 2 seconds, the callback (console.log('Inside setTimeout')) moves to the callback queue.
  • •Event Loop: Pushes the callback to the call stack after the main thread finishes.

Output:

Start
End
Inside setTimeout

Examples

Understanding Web APIs and Event Loop:

console.log('Start');

setTimeout(() => {
  console.log('Inside setTimeout');
}, 2000);

console.log('End');

Interview guide discussion

(0)

Join the discussion — login to comment and help other developers.

Login to comment