JavaScript $q.all promise depends on result of another promise

I currently have three nested promises which I want to turn into a $q.all call.

It looks something like this.

ds.saveData(data).then(function (result1){
    someOtherVar = result1.Id;
    ds.saveSomethingDependant(someOtherData).then(function (result2){
        ds.saveAThirdThing(someOtherVar).then(function (result3){
            ns.notify();
        }, function (error){
            ns.error(error);
        });
    }, function (error){
        ns.error(error);
    });
}, function (error) {
    ns.error(error);
});

Wow what a mess. My concern is where I edit someOtherVar. Is there some other way to do this so I don't have nested promises but still edit data after one promise? Nested promises are a nightmare to do jasmine tests on.

Thanks!

Answer:1

Since your result2 has a dependency on result1, you'll have to wait until the latter's ready before executing the former. But, you can still use $q.all if the first and third operations aren't dependent on one another:

$q.all({
  saveData: ds.saveData,
  saveAThirdThing: ds.saveAThirdThing
}).then(function(results) {
  /* expect(results).toEqual({
   *   saveData: result1,
   *   saveAThirdThing: result3
   * });
   */
  return saveSomethingDependent(results.saveData.Id);
}).then(function(result2) {
  // all done
  ns.notify();
}, function(error) {
  // something in the chain above failed
  ns.error(error);
});

We're taking advantage of the fact that you can return a promise from a resolve handler, which will then resolve that promise before sending it to the next promise in the chain (and thereby avoiding nesting).

You can also simplify somewhat by putting your reject handler at the end of the chain, since you're handling all errors in the same way in your example.

Answer:2

I'm trying to make a tooltip using jQuery, with HTML and CSS. Each tooltip is different by the id and that works great, so I can make as many tooltips as I want and style them independently. What I ...

I'm trying to make a tooltip using jQuery, with HTML and CSS. Each tooltip is different by the id and that works great, so I can make as many tooltips as I want and style them independently. What I ...

This is my first question on Stackoverflow so please excuse any mistakes I make. I'm trying to display in the view, some HTML from a template, which I use in the following directive: app.directive('...

This is my first question on Stackoverflow so please excuse any mistakes I make. I'm trying to display in the view, some HTML from a template, which I use in the following directive: app.directive('...

  1. html user insert image

I'm trying to open a remote video (let's say it's located at http://www.example.com/video.mp4) with the default Android player launched directly from Google Chrome, making use of the brand new intent:/...

I'm trying to open a remote video (let's say it's located at http://www.example.com/video.mp4) with the default Android player launched directly from Google Chrome, making use of the brand new intent:/...

  1. open video react native

This code works fine on IE, but fails on Chrome. Theory: When you click on the input, the input marks with an X or when you hit again the X is deleted (just like a checkbox) , when any of these ...

This code works fine on IE, but fails on Chrome. Theory: When you click on the input, the input marks with an X or when you hit again the X is deleted (just like a checkbox) , when any of these ...

  1. javascript code works with alert
  2. javascript code only works in debug
  3. how javascript code works
  4. javascript code only works with alert
  5. javascript code doesn't work
  6. javascript code doesn't work in chrome
  7. javascript code won't work
  8. javascript code dont work
  9. how javascript code coverage works