0

this post extends a previous one already solved. Please see it, to get in context: Nesting promises with $resources in AngularJS 1.0.7

With that approach in mind, I would like to make a parallel call to functions and when both are completed then run the searchBoats function with the results. I think I can nest the promises, but I also think it can be done in parallel, maybe with $q.all. Unfortunately, this promises topic is confusing for me and I don´t know how to do it.

Taking the previous post example, I would like to do something like:

var parseURL = function() {
  var deferred = $q.defer();
  var promise = deferred.promise;
  promise.then(function success(result) {
    console.log(result);
    searchBoats(result);
  });

  // Instead of resolve when parseBoatType promise is returned, I would like to   
  // wait for two promises
  parseBoatType().then(deferred.resolve);
  parseDestination().then(deferred.resolve);
  // of course, with this code deferred will be resolved when first promise 
  // comes. Is it possible to use $q.all here?
};
parseURL();
Rober
  • 5,868
  • 17
  • 58
  • 110
  • I don't understand, I showed you how to use `$q.all()` in your first question. You need to be returning a Promise in whatever functions you need to wait on and give `$q.all()` an array of the promises returned by those function calls that you want to wait on. – zero298 Sep 21 '17 at 17:03

1 Answers1

1

As in your earlier question, there is no need to use a deferred here. Just use $q.all(), which returns a promise:

var parseURL = function() {
  $q.all([parseBoatType(), parseDestination()])
      .then(function (results) {
          console.log(results);
          searchBoats(results);
      });
};
parseURL();
JLRishe
  • 99,490
  • 19
  • 131
  • 169