Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Promise.all() execute an array of functions or do they execute when you put them into the array?

Since await does not work inside Array.map or Array.reduce, can you do something like the following or would this be considered misuse of Promise.all? Normally, neo4j.session() would be awaited.

// inside a function

const QUERY = 'MATCH (n) RETURN n'
const argsArray = [{ sample: 'sadf' }, { sample: 'sadf' }, { sample: 'sadf' }]

const runQueries = argsArray.map(obj => neo4j.session.run(QUERY, obj.sample))

await Promise.all(runQueries)
      .then(results => results.forEach(result => console.log(result)))
like image 689
agm1984 Avatar asked Oct 25 '17 20:10

agm1984


People also ask

Does promise all return an array?

all() The Promise. all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises.

Does promise all execute all promises?

Promise. all is actually a promise that takes an array of promises as an input (an iterable). Then it gets resolved when all the promises get resolved or any one of them gets rejected.

What does promise () method do?

The . promise() method returns a dynamically generated Promise that is resolved once all actions of a certain type bound to the collection, queued or not, have ended. By default, type is "fx" , which means the returned Promise is resolved when all animations of the selected elements have completed.

Does promise all stop execution?

If any of the promises gets rejected, the Promise. all execution stops. So, how to handle such scenarios and make sure that even if a promise gets rejected, the execution of other promises doesn't stop.


2 Answers

Does Promise.all() execute an array of functions?

No its an array of promises

or do they execute when you put them into the array?

Exactly, When you build the Promises they're executed.

would this be considered misuse of Promise.all?

No this is totally fine, its actually the point of Promise.all.

However you might do (one after another instead of parallel execution) :

(async function(){

for(const obj of argsArray)
  console.log( await neo4j.session.run(QUERY, obj.sample));

})()
like image 174
Jonas Wilms Avatar answered Oct 24 '22 10:10

Jonas Wilms


async.await is supposed to be syntactic sugar for sequential promise chains. Considering that database queries are supposed to run concurrently, it's perfectly fine to use await Promise.all(...) in such cases.

Promise.all accepts an array of promises (more specifically, an iterable), and promises start to execute at the moment when they are created. It may be the moment prior to Promise.all call:

const promises = [Promise.resolve(1), Promise.resolve(2)];
// promises have been created at this point
Promise.all(promises).then(...)

Or it may be not. If an iterable is not an array but a generator, promises will be lazily created during Promise.all call:

const promiseGen = (function* () {
  yield Promise.resolve(1);
  yield Promise.resolve(2);
})();
// promises have not been created yet at this point
Promise.all(promiseGen).then(...)
// promises have been created
like image 21
Estus Flask Avatar answered Oct 24 '22 10:10

Estus Flask