0

I am new at Javascript and I have a problem when I want to get list of model by its ID. I have read about "promise" but I do not know the right way to use it in this case. This is my code

var ids = [1, 2, 3];

var cars = [];
ids.forEach(function (id, index, array) {
    Car.findOne({_id: id}, function(err, car){
        cars.push(car);
    });
});

return cars;

Help me please. Thank you all!

Duy
  • 1
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Joe Clay Jun 23 '17 at 15:40
  • Thank you. It works – Duy Jun 26 '17 at 07:03
  • Excellent :) I think working with asynchronous code trips up pretty much everyone to begin with, glad you were able to get things working. – Joe Clay Jun 26 '17 at 08:19

0 Answers0