A
A
Adel Khalitov2019-09-03 16:08:55
JavaScript
Adel Khalitov, 2019-09-03 16:08:55

How to run a method in parallel with server operation?

There is a method that works for 40 minutes:

let promises = some.map(el => {
   return new Promise((res ,rej) => {
       ...some action
   })
})

await Promise.all(promises)

So if there are requests to the server in the process of execution, it will wait for the end of this 40-minute method, and only then will it process the requests. How to avoid it?
Any ideas how correct this is?
Let's call the code described above updateDB, it has a parent method that calls it, conditionally comareData. Will this be the right decision?
comareData() {
   return new Promise(async (res, rej) => {
       await someOtherFunctions();
        ...
      updateDB()
          .then(data => res(data))
          .catch(err => rej(data))
   })
}

comareData(); //Далее запуск без тега await

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2019-09-03
@vshvydky

IMHO this is not correct, you must have a server independent of any workers to work with
the database IMHO working with the database as a whole can lead to blocks of the database itself as a result of denials of service. it is worth analyzing separately.
suppose your server should receive a command to start any such action, ok, then do it through the queue, through the second server that receives the command from the first one, whatever, as long as you don’t lock your main process
and yes, your comareData function is a masterpiece of misunderstanding of promises and async functions, this gap is a reason to fix it.
at least now I see that your promise will never resolve if someOtherFunctions throws an exception

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question