top of page
Logo der Online Agentur mdwp

Callback Queue

A Callback Queue in React JS is basically a notion used in respect of the Event Loop, which is a core concept of JavaScript. When a task is completed like an API request, an event like a mouse click, or a timer is finished, a callback associated with it is sent to the Callback Queue. Each of these callbacks is then processed and moved onto the Call Stack when the Stack is clear. Only then is the callback executed.

The Callback Queue in React can play an important role in optimizing performance. React actually uses two kinds of queues: first one is main queue where all state updates and re-renders happen, and the second one is a callback queue where all the side-effects (I/O tasks like API calls, Event Handlers etc.) happens after state updates and paint.

Here is a generic example of how callbacks are added to the Callback Queue:
```JavaScript
setTimeout(() => { console.log("This reprents a callback in the Callback Queue."); }, 2000);
```

In this code, we have declared a function after a delay of 2 seconds (2000 milliseconds). Whenever we're using asynchronous JavaScript operations like setTimeout, the callback function isn't executed immediately but instead is sent into the Callback Queue, and hence it demonstrates the notion of the Callback Queue.

bottom of page