编程语言
首页 > 编程语言> > javascript – Promise.all()是执行函数数组还是在将它们放入数组时执行?

javascript – Promise.all()是执行函数数组还是在将它们放入数组时执行?

作者:互联网

由于await在Array.map或Array.reduce中不起作用,您可以执行以下操作,还是将其视为滥用Promise.all?通常,等待neo4j.session().

// 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)))

解决方法:

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

没有一系列的承诺

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

确切地说,当你构建Promises时,它们就被执行了.

would this be considered misuse of Promise.all?

不,这完全没问题,它实际上是Promise.all的重点.

但是你可能会(一个接一个而不是并行执行):

(async function(){

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

})()

标签:javascript,ecmascript-6,async-await,ecmascript-2017
来源: https://codeday.me/bug/20190608/1195834.html