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.
-
$timeout
already returns a promise, no need for a$q.defer
there – 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
1 Answer
Reset to default 10Of 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
版权声明:本文标题:javascript - AngularJS : Pass additional parameters to chained promises - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741503677a2382194.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论