最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Looping with Promises in JS - Stack Overflow

programmeradmin1浏览0评论

I am trying to run queries inside a for loop in nodejs and each iteration of the for loop runs one query. The queries are done with a "get()" method that returns a Promise. In my function (that contains this for loop), I am returning the promise returned by the get() method like so:

for(var i=0; i < productIds.length; i++) { return queryObject.get(productIds[i]).then(//handle result) }

but this returns after the first iteration and doesn't continue for the rest of the iterations. How do I solve this issue?

For context, I am using Parse's Cloud Functions.

I am trying to run queries inside a for loop in nodejs and each iteration of the for loop runs one query. The queries are done with a "get()" method that returns a Promise. In my function (that contains this for loop), I am returning the promise returned by the get() method like so:

for(var i=0; i < productIds.length; i++) { return queryObject.get(productIds[i]).then(//handle result) }

but this returns after the first iteration and doesn't continue for the rest of the iterations. How do I solve this issue?

For context, I am using Parse's Cloud Functions.

Share Improve this question edited Sep 27, 2018 at 16:42 krishnakeshan asked Sep 27, 2018 at 16:31 krishnakeshankrishnakeshan 1,2802 gold badges15 silver badges19 bronze badges 11
  • Share a bigger code snippet. – Evert Commented Sep 27, 2018 at 16:32
  • I am away from my puter atm but I'm happy to answer any queries you have – krishnakeshan Commented Sep 27, 2018 at 16:34
  • Come back later and share more code. – Evert Commented Sep 27, 2018 at 16:37
  • why the async-await tag – zebnat Commented Sep 27, 2018 at 16:37
  • I have updated the question, hopefully it gives you little more information – krishnakeshan Commented Sep 27, 2018 at 16:43
 |  Show 6 more ments

3 Answers 3

Reset to default 4

You have to build up a promise chain like so:

let chain = Promise.resolve();

for (i=0; i<1000; i++) {
   chain = chain.then(()=>queryObject.get(objectId))
}

chain.then(()=>{ /* do something once all queries have returned a result */})

The answer given by TLP demonstrates Promise Chaining, and will work for you here. However, I'll explain a little more about what is happening and provide an alternative answer.

First, let's understand why your code fails to do what you want:

for(var i=0; i < productIds.length; i++) {
    return queryObject.get(productIds[i]).then(//handle result)
}

You want the code to run for every productId; however, you are telling the code to return, exiting the for loop, in the very first iteration. Think about the following:

for(var i=0; i < productIds.length; i++) {
    return productIds[i];
}

Removing the promises from the picture makes it a bit easier to see the error. This for loop is equivalent to:

return productIds[0];

Likewise, your code is equivalent to:

return queryObject.get(productIds[0]).then(//handle result)

Assuming you see the error here, now I'll point out a small error in your thought process that got you here. In your post you say:

I am returning the promise returned by the get() method like so:

You refer to a single promise returned from "the" call to the get() method, but you are actually hoping to call the get() method many times, producing many promises.

If all of this makes sense then your next step is to decide whether:

  1. You need to run code after ALL promises have pleted (that uses the full list of retrieved data).
  2. You only need to run code per pleted promise (that uses only the data retrieved from the specific promise).

You achieve #1 using Promise Chaining, as illustrated by TLP's answer. You can achieve #2 by simply removing the "return" from your code, i.e.:

for(var i=0; i < productIds.length; i++) {
    queryObject.get(productIds[i]).then(//handle result)
}

Two possibilities, parallel and serial. I'll show both:

async function serial(productIds) {
  for(const productId of productIds) {
      await queryObject.get(productId).then(//handle result)
  }
}

function parallel(productIds) {
  return Promise.all(
     productIds.map( productI => queryObject.get(productId).then(//handle result)
  );
}
发布评论

评论列表(0)

  1. 暂无评论