1
To illustrate the problem let’s go to a real scenario: I have to access something close to 450 directories shared in network to get the metadata of a given file, this data will be later compared with those of the original file.
In the above scenario I search the information of size and date/time (timestamp) of the last modification, to obtain this data is simple, basically I call this module:
const fs = require('fs')
const metadata = (path) => {
return new Promise((resolve, reject) => {
fs.stat(path, (err, stats) => {
if (err) reject(err)
resolve(stats)
}
})
}
module.exports = metadata
The point here is that I need this operation to be performed in parallel (at this point I’m referring to background execution via thread pool), that is, I can’t block the flow with a await, so much so that I’m even using set UV_THREADPOOL_SIZE=10 to increase the amount of thread...
I have tried numerous solutions, in one of them I call the module presented above and store the Promise returned in an array, just ahead a loop for traverse the array searching for each Promise and calling one then, internally this instruction performs the comparison with the source, stores the result whether it is equal or not in an array and uses a if to determine if this was the last Promise of the Array, if yes print on the console the final result.
The problem is that I did not find a way to execute these queries in parallel, obtaining only the consolidated result. Roughly speaking, I was unable to synchronize asynchronous operations with synchronous sections in order to take advantage of both. How can I query the data in parallel and then synchronously execute data instructions?
Ever tried to use
Promise.all? By itself, Javascript is not a language that allows parallel execution. Learn more about this here. In Node.js, you can useworker_threads, to create other running threads.– Luiz Felipe
Yes, I’m aware of that. I will modify the question to clarify this point, in fact I am working with a higher thread volume than the standard to meet the amount of queries, especially considering that some of them may get stuck until a timeout of the call occurs. In the case Promise.all does not fully meet me since there is no guarantee that all Promises will be resolved.
– Fábio Jânio
If the environment supports, you can use
Promise.allSettled.– Luiz Felipe
I know, and I understand, that your goal is to get a closed Javascript solution, but if you don’t get a solution you have already considered a Python server assisting Node.js by scheduling and running these tasks in parallel.
– Augusto Vasques
I am closed in Javascript more by necessity, unfortunately I was not given the possibility to use other technology. But personally I would definitely consider it. Now I’m looking at the Worker API, see if I can get a light.
– Fábio Jânio