node.js

소바·2022년 8월 29일
0

NodeJS is a JavaScript Runtime built on Google's open-source V8 JavaScript engine. what the JavaScript Runtime

Now, who actually does execute the code if not the browser?
And that's where the V8 engine


https://nodejs.org/dist/latest-v16.x/docs/api/repl.html

FS here stands for file system.
So by using this module here, we will get access to functions for reading data and writing data right to the file system.
So again, calling this function here with this built-in FS module name will then return an object in which there are lots of functions that we can use.

synchronous vs Asynchronous

synchronous

So you see that each line of code basically waits for the result of the previous line.

Now, this can become a problem, especially with slow operations, because each line blocks the execution And so, we say that synchronous code is also called blocking code.

Asynchronous

So the solution to this problem in Node is to use asynchronous, non-blocking code.

So in asynchronous code, we upload heavy work to basically be worked on in the background.

And then, once that work is done, a callback function that we register before is called to handle the result.

the rest of the code can still be executing without being blocked by the heavy task, which is now running in the background.

So what this means is that we can effectively defer or reaction into the future.

Why is it needed?

why does is actually have to be this way?

What's the problem with blocking code execution in Node.js?
Or, in other words, why do we actually use callback so many times in Node.js?


Let's see the fact that a Node.js process, which is where our application is running.

there's only one single thread.

And the thread is just like a set of instructions that is run in the computer's CPU.

Now, what that means is that all the users accessing your application are all using the same thread, so, basically, accessing the same thread.

what this also means is that when one user blocks the single thread with synchronous code, then all other users will have to wait

So, for the same situation, we should, of course, use the asynchronous file read function, which instead of blocking the single thread, does the heavy work in the background, where it basically stays until it's finished reading the data from the file.

Of course, we then also register a callback function to be called once the data is available.

one after another, while the file is still being read in the background.

Now, once the data is read, our callback function will, of course, get called to be executed in the main single thread in order to process the read data.

That's an overview of how Node.js handles asynchronous behavior in order to implement the non-blocking I/O model that we talked about

I / O ?

And I/O simply stands for input-output, which is basically stuff like accessing the file system and handling network requests.
This is actually the whole reason why Node.js is completely designed around callbacks,
In other programming languages, like PHP, it works very differently because you get, basically, one new thread for each new user, which is a completely different paradigm and really works completely different.

callback ≠ Asynchronous

that doesn't automatically make it asynchronous, all right?
So, passing functions around into other functions is quite common in JavaScript,
It only works this way for some functions in the Node API, such as the readFile function

Callback hell

So, this callback model that we just discussed, where one function is called once the one before has finished it's work,
can quickly lead to some hard to read and unmanageable code.

Just take this example where the second file read Just take this example where the second file read depends on the first one, then, the third file read depends on the second one, and finally, we want to use the final data.

Anyway, this is what we call the callback hell. It's such a common problem, And do you notice this triangular shape here?

That's a very clear sign that you're in callback hell.

Now, how do we actually escape callback hell?

Well, we can use more advanced tools for handling asynchronous code, like ES6 promises or async/await.

비동기처리의 과정

profile
소바보이

0개의 댓글