K
K
klaod2017-01-25 00:17:31
JavaScript
klaod, 2017-01-25 00:17:31

What are threads in node.js?

Guys explain in an accessible way what are streams in node.js and what are the advantages of using them?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kitmanov, 2017-01-25
@klaod

This is such a thing that gives out (or receives, or both) data in chunks (chunk). To explain why they are needed, we need to digress a bit and remember how nodejs works.
The advantage of nodejs is that while the database or file system is responding to the command, we can start another callback. Due to this, we can process many requests in parallel, but from the point of view of the programmer, there is no parallelism, there are no threads, the race condition does not arise, and his head does not hurt about it. And everything works fast for us, as long as no function takes the processor for too long, as long as it does not work for too long. And if it takes, then, due to single-threading, all our other requests will hang.
But sometimes we still do a lot of work - read or write a large file, for example. With the help of threads, we split this work into pieces that do not take a percentage for a long time. This is not always possible, but in a large number of cases it works for itself.
PS This answer is about stream (not to be confused with thread).

R
Rossomah, 2017-02-04
@Rossomah

Вот здесь подробно описаны потоки и работа с ними в Node.js
https://makeomatic.ru/blog/2016/08/22/Streams_handbook/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question