Using the Promise versus NOT using the Promise: in what cases?

I don't know if SO is suitable for such a question.

I know a few Promises , and I use them in a Node/Express environment to “fix” Node asynchronous behavior when querying a database (= Wait for the DB to respond, then do something).

However, the more I use them, the less I know when I do not use them.

For example, I wrote a code fragment like this (for a local Google Matrix API request script) ...

  .... for (var i = 0; i < rows.length; i++) { // loop over a CSV file by line var cell = rows[i].split(/;/) var origin = cell[1] var destination = cell[2] var id = cell[0] Promise.all([origin, destination, id]).then((p) => {} ... 

I do not know if it makes sense to use Promise.all here ...

Is there a rule to know? Behavior that I do not get?

Speak differently when I know that there is a “risk” that my script runs a function without its correct argument ( argument returns from another function that is not “over”) ..

Thanks.

+5
source share
3 answers

If you have completely synchronous code (without the asynchronous operations that you are trying to track completion), you DO NOT want to use promises.

Trying to use promises with synchronous code only adds unnecessary complexity to the logic of the code and slows down the execution of your code. When using all the synchronous code, there is nothing to wait for completion or agreement with the completion, so you can simply execute your code and immediately get the result. Adding promises just complicates your code unnecessarily.

In addition, Promise.all() expects you to pass it an array of promises, and not an array of values ​​similar to the one you are doing, in addition to the unnecessary, what you did is also wrong.

So, there is no reason to not just do this:

  .... for (var i = 0; i < rows.length; i++) { // loop over a CSV file by line var cell = rows[i].split(/;/) var origin = cell[1] var destination = cell[2] var id = cell[0] // operate on id, destination and origin here // no need to wait on a promise before doing so } ... 

The promise will be used with asynchronous operations, such as database operations, file operations, network operations, synchronization operations, etc. You can use them to know when a single asynchronous operation is being performed or to coordinate several operations (some of which are asynchronous).

+2
source

I usually use promises if I use an asynchronous process, such as a call to retrieve data from a database. I cannot think of what you will need to use a promise if you are not dealing with asynchronous processes.

+3
source

Typically, you do not use your own promise yourself, you use the promise because what you are currently using requires some asynchronous process:

  • client client http
  • database / other server side query

The connectors / libraries you usually use already impose a promise or callback on you, in which you can simply resolve the promise to use the promise everywhere.

Another very specific kind of asynchronous process is when the client side does some heavy thing (which usually should not be!), And you need to make it as if it works in the background and resolves it asynchronously.

You can use the promise and decide it when it is done.

Take a look at this SO post if you don’t understand what I meant by “running in the background”, since Javascript is one-way, there isn’t such a thing, it's just emulation.

+1
source

Source: https://habr.com/ru/post/1261998/


All Articles