V
V
veryoriginalnickname2021-08-25 22:06:11
Node.js
veryoriginalnickname, 2021-08-25 22:06:11

In what cases to do asynchrony?

Let's say we have a server running Node. For example, a request with user data comes to the controller, and the controller calls a function that validates this data. This validator function checks whether the password/username is greater or less than so many characters, also calls some additional functions, in short, the validator function uses the processor (i.e. does not send other requests, does not write something to disk, and so on ). Then this function returns a true or false response to the controller, and then the controller, depending on the bool, returns a response to the user.
Should the validator function be made asynchronous? When should a function be made asynchronous on a Node server?
For example, bcrypt also hashes asynchronously. But bcrypt only uses the CPU, right? And the validator function, which compares and checks the values, also uses the processor. Where is the line when you need to do async?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2021-08-25
@veryoriginalnickname

bcrypt takes CPU time for half a second by executing a function, your code for fractions .... if, honestly, the node's event loop executes only 1 function at a time, which means that in general for the node, using long-executing functions in the main thread will be painful. in the case of the validator, no "asynchrony" is needed. By the way, about the bcrypt, if I remember correctly, it is implemented as a C-addon and is executed in the lib threads and not in the main node thread, so the asynchrony is justified there.
to understand what and how is done in a node, study the node's event loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question