It depends on the control flow. Node.js focuses on asynchrony, and one of the main disadvantages of asynchrony is that the code does not flow in such a way that you can use it with a synchronized language.
In synchronous language, the caller is blocked while the function is waiting for some data. This makes programmers work quite simply because they can be guaranteed that when a function is waiting for data to return, data will be available to the caller.
This is the exact opposite in asynchronous language or with non-blocking I / O. In this case, the caller is blocked for the duration of the function call, however, the functions should not wait for the completion of data entry or I / O before returning. This complicates the programmer a bit, because when the function call returns, there is no guarantee as to whether the data will be available. Therefore, non-blocking I / O usually involves callback functions that are called when available data is available.
try/catch blocks the work with the call stack. That is, when an exception is thrown, the runtime expands the call stack until it finds a catch that surrounds the call that caused the exception. But since http.get is a non-blocking call, it exits immediately after registering some callbacks, and processing continues. Callbacks are called in a separate "thread", and so the calls are not nested in the original try/catch .
The chart will really help explain things here, but unfortunately I do not have one available to me.
Bryan kyle
source share