Introduction
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.
Q1. Create a sleep function using Async/Await.
In the below example, the sleep function returns a promise which is resolved after given millisecond using setTimeout API.
Q2. How Javascript Engine works?
When a js file is ran,
A global execution context is created.
A memory heap is created where all variables (with var keyword) and functions (except arrow func) are declared.
In the below image we can see, variable a is declared w/o even execution has started.
A Call Stack is created which stores the line which is going to be executed.
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.
Q3. Difference between Single Threaded & Asynchronous.
My View:
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
Q4. How to debug a slow API?
My View:
The Console.log approach
- 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.
Profiler Approach
Use any profiling tool to check where the execution is lagging.
What is EventEmitter in NodeJS?
My View: EventEmitter is used to create event listeners in JS.
Read More:
Top comments (8)
Nice article, and I like the illustrations.
I think there's one particular thing that might be a little skewed, but feel free to correct me if I'm wrong.
My opinion on the single-threaded is, instead of "one function executed at a time," it would be any given synchronous code piece, regardless of it is in a function or not.
The example that comes to mind for that is if you had a never-ending do-while loop and were repeatedly calling a function on each iteration, any asynchronous code which hasn't yet run, which was defined before that loop was set, will probably not get the chance to run as it would probably be blocked by the synchronous loop iterations.
Hey thanks good article. But to be nitpicky, that is because async in JavaScript isn't really asynchronous, in most other languages you would expect async code to be on a different thread and be able to run simultaneously. And in JS all code runs on a single thread so I'd say single thread vs async is a bit out. It should be syncronous Vs async. And in the answer I would explain as jzombie said that even async code is on the single thread and can block other async functions from running.
Yes the question was in context of JS, because JS is single threaded, async language. So the question is what's the difference between the two.
Will modify the question as well as answer for better understanding.
Thanks for the appreciation and correction.
Will correct it 🙌🏼
nice article.
I think it should be sync vs async and single threaded vs multi threaded.
In your example response 1 and response 2 can be in any order. it worth mentioning that too.
Thanks sami for the appreciation.
Will update a/c to the feedback :)
Amazing article really helpful!
Thanks for the appreciation Lavish :)