Node JS can be confusing for any beginner or for those who are coming from any other programming language. Most developers find it difficult to understand the flow of code in node js scripts. In this post i am gonna make it super simple to understand how a piece of code runs in Node JS script.
The reason behind this Non-blocking behavior is the Beautiful event loop running behind the scenes in Node JS. Without getting much in details, the event loop can be explained as a watchman which keeps an eye on every command given to node js and put it in action. Here commands are each line of code. Event put all the line of code to action even if previous line is not completed yet.
There are situations where we don’t want this ‘Async’ behavior and the best example of that is when we make an api call, in that case we want the result from the api call and then we want to go to the next line to process the response further. We have multiple options to handle these situations in node js or in js.
First Method: The first and the oldest way is the call backs, callbacks are functions which we trigger once we are done with our first function. In simple words, let’s say you have two lines of code and you want to execute the second line only after the first line is completed. And the first line is taking time as it is dependent on some other functions, what you can do is wrap the second line in a function and pass this function to line 1 function itself.
If i want to make an api call and then after that using the response i want to build a json on that response. So I make a function of the process of creating a json and pass that function to the api and ask that api to call that function once you are done. I have made a video explaining and coding the same, you can check same for better understanding.
Second Method: The cooler and prettier way to handle any async situation is a promise. In the simplest words I can explain, a promise is an object which you can tell what to do after you are done with your work. We can tell promise two different outputs in case of success and failure of the main function.
A Promise can be in one of these states:
- pending: initial state, neither fulfilled nor rejected.
- fulfilled: meaning that the operation was completed successfully.
- rejected: meaning that the operation failed.
For example, if you are calling an api, in that case, your api should be a promise and you can tell the promise, what you want to do if your api runs successfully or what you want to do if it fails. I have made a video explaining and coding the same, you can check same for better understanding.
Third Method: The cleanest way of handling async node js is “async await”. In this method you just don’t pass what you want to do after completion of the current function, you stop the compiler on the given line and wait until it gets completed. This waiting is done inside a function and that function should be explicitly async, that’s why this name came as “Async Await”. Although, in the newest version of node js, “Async” function is not required to await on a particular line.
Now explaining this with an example: if you are calling an api, in that case, your api should be a promise and you can tell this calling to wait at this line until you get the response and then go to the next line where you can play with the response. I have made a video explaining and coding the same, you can check the same for better understanding.
Hope you understood how you can deal with async node js with this blog. Do follow me for more content and do checkout my YT channel