Hello, I’m writing a thesis on nodzhs I write and the following question arose: in theory, everything should be non-blocking, and functions should be called solely by callbacks, or by events, but there are such small functions, such as counting the sum of an array (conditionally), which are performed very quickly and for their sake you do not want to cut one large, beautiful, logical function into a chain of callbacks. So this is: can such small functions be left with retourns, and wait for an answer from them, like they are inline type?

  • 3
    not quite everything, all IO operations - Zowie
  • That is, for example, I have a piece where a bunch of mathematics is considered. Mathematics is the lot of the processor, there is nowhere access to the disk, so there might be as many function calls with retourns inside that are also only in RAM? - AntoNi0
  • yep Imagine how you wrote the code before. There's absolutely atomic noodles, right? - Vladimir Gordeev
  • =) No, this is my first project on nodzhs, and from the very beginning I did all the mathematics with retourns because I didn’t really stick in the asynchronous style, then I read it, googled, and thought to sleep that everything, pipets, everything had to be redone, but in the morning, with a fresh mind, I realized that this is just a piece of code, as it were, the whole code comes out and everything, and redid only the moments of accessing the database and the cache. - AntoNi0

1 answer 1

I can advise you to google about the words "Node.js is a cancer". There was such a provocative article , after which everyone rushed to write the server to calculate the Fibonacci numbers, measure the results and argue that this is more correct. Here, for example, https://github.com/glenjamin/node-fib

If I correctly understood the idea, the point is that with an intense flow of heavy (mathematical) queries, the time spent on calculating the answer becomes not so important. Anyway, the answer you will calculate a long time. It is much more important that clients receive a response at the same time, and for this very reason you need to use many asynchronous calls.

  • the point is that the Node script parses the Internet for pictures, and then performs mathematical operations on these pictures. But in the same thread, there is a web server that must handle user requests. - AntoNi0
  • 3
    I would generally advise to split into two parts. Web-muzzle separately, parser-processor separately. Between themselves, if necessary, communicate with any of the myriad IPC methods. - drdaeman
  • I support the previous commentator. Judging by your descriptions, these are two different tasks, just requiring the same database. - Shad