admin管理员组

文章数量:1290971

I want to chain some promises that are returned by services. This works, as long as some of the methods that return the promises, doesn't require additional parameters. This is my example:

var first = function() {
  var d = $q.defer();
  $timeout(function() {
    d.resolve("first resolved")
  }, 100)
  return d.promise;
};

var second = function(val) {
  console.log("value of val: ", val);
  var d = $q.defer();
  $timeout(function() {
    d.resolve("second resolved")
  }, 200)
  return d.promise;
};

first().then(second).then(function(value) {
  console.log("all resolved", value);
});

This works as expected. But what if my service second needs an additional parameter val to do it's job? With the method above the value of val is "first resolved", because it get's the resolved value from first.

Is there any way around, without nesting anonymous functions like this:

first().then(function() {
  return second("foobar").then(function(value) {
    console.log("all resolved", value);
  });
});

I was thinking about using $q.all, but IMHO you can't specify an order for your promises.

I want to chain some promises that are returned by services. This works, as long as some of the methods that return the promises, doesn't require additional parameters. This is my example:

var first = function() {
  var d = $q.defer();
  $timeout(function() {
    d.resolve("first resolved")
  }, 100)
  return d.promise;
};

var second = function(val) {
  console.log("value of val: ", val);
  var d = $q.defer();
  $timeout(function() {
    d.resolve("second resolved")
  }, 200)
  return d.promise;
};

first().then(second).then(function(value) {
  console.log("all resolved", value);
});

This works as expected. But what if my service second needs an additional parameter val to do it's job? With the method above the value of val is "first resolved", because it get's the resolved value from first.

Is there any way around, without nesting anonymous functions like this:

first().then(function() {
  return second("foobar").then(function(value) {
    console.log("all resolved", value);
  });
});

I was thinking about using $q.all, but IMHO you can't specify an order for your promises.

Share Improve this question edited Sep 29, 2015 at 13:01 John Slegers 47.1k23 gold badges204 silver badges173 bronze badges asked Jul 23, 2014 at 11:52 23tux23tux 14.7k16 gold badges96 silver badges194 bronze badges 3
  • $timeout already returns a promise, no need for a $q.deferthere – Benjamin Gruenbaum Commented Jul 23, 2014 at 14:09
  • I know, it was just for demonstration purpose. In my real case, I have a http call and after that, do some processing with the data. So I do need an extra $q.defer – 23tux Commented Jul 23, 2014 at 14:15
  • uhh... $http also returns a promise already and you can .then it for the extra processing and return that... can't make a judgement about code I haven't seen but it still sounds like the deferred anti pattern. You only need $q.defer when working against a callback API when promisifying it. – Benjamin Gruenbaum Commented Jul 23, 2014 at 14:16
Add a ment  | 

1 Answer 1

Reset to default 10

Of course. First way:

first()
  .then(function() {
    return second("foobar");
  })
  .then(function(value) {
    console.log("all resolved", value);
  });

Second (much easier) way:

first()
  .then(second.bind(null, "foobar"))
  .then(function(value) {
    console.log("all resolved", value);
  });

本文标签: javascriptAngularJSPass additional parameters to chained promisesStack Overflow