Being a Full Stack Developer, I had appeared for Backend Developer position as well. In this post, I will be sharing most common Backend Dev question asked to me in all those interviews except the DSA part.
In the below example, the sleep function returns a promise which is resolved after given millisecond using setTimeout API.
When a js file is ran,
A global execution context is created.
When call stack gets a function call, it creates a functional context and the process is repeated in the functional context.
When call stack encounters WEB APIs like setTimeout/Interval/Immediate, these are sent to callback queue to process after call stack is empty.
When call stack encounters a Promise it is sent to micro task queue (higher priority than callback queue) to process after call stack is empty.
Once call stack is empty, event loop moves functions from queues to call stack for execution.
Single Threaded -> It means only once function can be executed at a time.
Async -> It is about non-blocking of execution of functions.
In this below image, we can easily understand the difference. Thanks to Baeldung
- Check if the issue is with network by sending request from different network.
- Log time when req is received by the backend.
- Log time before a DB query.
- Log time after a DB query.
- Log time before/after some heavy operation.
- Log time before sending response.
- Analyse the time, you will get your culprit.
Use any profiling tool to check where the execution is lagging.
My View: EventEmitter is used to create event listeners in JS.