如何处理循环中的promise?

时间:2022-01-12 11:05:17

This is what I would like to do

这就是我想做的

var response = [];

Model.find().then(function(results){
   for(r in results){
      MyService.getAnotherModel(results[r]).then(function(magic){
          response.push(magic);
      });          
   }
});

//when finished
res.send(response, 200);

however it returns just [] because the async stuff is not ready yet. I am using sails.js that uses Q promise. Any ideas how to return a response when all async calls are finished?

然而它只返回[],因为异步的东西还没有准备好。我正在使用使用Q promise的sails.js。任何想法如何在所有异步调用完成后返回响应?

https://github.com/balderdashy/waterline#query-methods (promise methods)

https://github.com/balderdashy/waterline#query-methods(承诺方法)

3 个解决方案

#1


6  

Since waterline uses Q, you can use the allSettled method.
You can find more details on Q documentation.

由于水线使用Q,您可以使用allSettled方法。您可以在Q文档中找到更多详细信息。

Model.find().then(function(results) {
  var promises = [];
  for (r in results){
    promises.push(MyService.getAnotherModel(results[r]));
  }

  // Wait until all promises resolve
  Q.allSettled(promises).then(function(result) {
    // Send the response
    res.send(result, 200);
  });
});

#2


3  

You simply can't do that, you have to wait for the asynchronous functions to complete.

您根本无法做到这一点,您必须等待异步函数完成。

You can either create something yourself, or use the async middleware, or use built in features, as noted in Florent's answer, but I'll add the other two here anyway :

你可以自己创建一些东西,或者使用异步中间件,或使用内置功能,如Florent的回答所述,但我还是会在这里添加其他两个:

var response = [];

Model.find().then(function(results){
   var length = Object.keys(results).length,
       i = 0;
   for(r in results){
      MyService.getAnotherModel(results[r]).then(function(magic){
          response.push(magic);
          i++;
          if (i == length) {
              // all done
              res.send(response, 200);
          }
      });     
   }
});

or with async

或者使用异步

var response = [];

Model.find().then(function(results){
   var asyncs = [];
   for(r in results){
       asyncs.push(function(callback) {
           MyService.getAnotherModel(results[r]).then(function(magic){
               response.push(magic);
               callback();
           })
       });
   }
   async.series(asyncs, function(err) {
       if (!err) {
           res.send(response, 200);
       }
   });
});

#3


-3  

Take a look at jQuery deferred objects:
http://api.jquery.com/category/deferred-object/

看一下jQuery延迟对象:http://api.jquery.com/category/deferred-object/

Specifically, .when()
http://api.jquery.com/jQuery.when/

具体来说,.when()http://api.jquery.com/jQuery.when/

#1


6  

Since waterline uses Q, you can use the allSettled method.
You can find more details on Q documentation.

由于水线使用Q,您可以使用allSettled方法。您可以在Q文档中找到更多详细信息。

Model.find().then(function(results) {
  var promises = [];
  for (r in results){
    promises.push(MyService.getAnotherModel(results[r]));
  }

  // Wait until all promises resolve
  Q.allSettled(promises).then(function(result) {
    // Send the response
    res.send(result, 200);
  });
});

#2


3  

You simply can't do that, you have to wait for the asynchronous functions to complete.

您根本无法做到这一点,您必须等待异步函数完成。

You can either create something yourself, or use the async middleware, or use built in features, as noted in Florent's answer, but I'll add the other two here anyway :

你可以自己创建一些东西,或者使用异步中间件,或使用内置功能,如Florent的回答所述,但我还是会在这里添加其他两个:

var response = [];

Model.find().then(function(results){
   var length = Object.keys(results).length,
       i = 0;
   for(r in results){
      MyService.getAnotherModel(results[r]).then(function(magic){
          response.push(magic);
          i++;
          if (i == length) {
              // all done
              res.send(response, 200);
          }
      });     
   }
});

or with async

或者使用异步

var response = [];

Model.find().then(function(results){
   var asyncs = [];
   for(r in results){
       asyncs.push(function(callback) {
           MyService.getAnotherModel(results[r]).then(function(magic){
               response.push(magic);
               callback();
           })
       });
   }
   async.series(asyncs, function(err) {
       if (!err) {
           res.send(response, 200);
       }
   });
});

#3


-3  

Take a look at jQuery deferred objects:
http://api.jquery.com/category/deferred-object/

看一下jQuery延迟对象:http://api.jquery.com/category/deferred-object/

Specifically, .when()
http://api.jquery.com/jQuery.when/

具体来说,.when()http://api.jquery.com/jQuery.when/