在Angular中用$ q链接承诺

时间:2021-09-28 03:26:30

I am trying to chain promises so that doQuery(0) executes then doQuery(1), etc... sequentially until doQuery(9).

我试图链接promises,以便doQuery(0)执行doQuery(1)等等,直到doQuery(9)。

My problem is that i is always equals to 10 in the callback function.

我的问题是我在回调函数中总是等于10。

doQuery(0) executes then doQuery(10).

doQuery(0)然后执行doQuery(10)。

How do I pass each value of i in the callback function?

如何在回调函数中传递i的每个值?

var promise = doQuery(0);
for (var i = 1; i < 10; i++) {
    promise = promise.then(function() {
        doQuery(i);
    });
};

2 个解决方案

#1


5  

Since you're using Angular.js, you should use it's bind function here:

既然你正在使用Angular.js,你应该在这里使用它的绑定函数:

var promise = doQuery(0);
for (var i = 1; i < 10; i++) {
    promise = promise.then(angular.bind(null, doQuery, i));
}

Without relying on Angular.js, you could use a closure to make a copy of i for each callback function (rather than having them all share the single copy of i in the outer scope):

如果不依赖Angular.js,您可以使用闭包为每个回调函数制作一个i的副本(而不是让它们在外部作用域*享i的单个副本):

var promise = doQuery(0);
for (var i = 1; i < 10; i++) {
    promise = promise.then(function(i){
        return function(){
            doQuery(i);
        };
    }(i));
}

In modern Javascript engines you can also use the native Function.prototype.bind:

在现代Javascript引擎中,您还可以使用本机Function.prototype.bind:

var promise = doQuery(0);
for (var i = 1; i < 10; i++) {
    promise = promise.then(doQuery.bind(null, i));
}

#2


2  

You need to return each to-be-chained promise from the then callback, otherwise it will likely fail. To pass the right i value to each callback, see JavaScript closure inside loops – simple practical example.

您需要从当时的回调中返回每个待链接的承诺,否则它可能会失败。要将正确的i值传递给每个回调,请参阅循环内的JavaScript闭包 - 简单的实际示例。

var promise = doQuery(0);
for (var i=0; i<10; i++) (function(ii) {
    promise = promise.then(function() {
        return doQuery(ii);
    });
})(i);

#1


5  

Since you're using Angular.js, you should use it's bind function here:

既然你正在使用Angular.js,你应该在这里使用它的绑定函数:

var promise = doQuery(0);
for (var i = 1; i < 10; i++) {
    promise = promise.then(angular.bind(null, doQuery, i));
}

Without relying on Angular.js, you could use a closure to make a copy of i for each callback function (rather than having them all share the single copy of i in the outer scope):

如果不依赖Angular.js,您可以使用闭包为每个回调函数制作一个i的副本(而不是让它们在外部作用域*享i的单个副本):

var promise = doQuery(0);
for (var i = 1; i < 10; i++) {
    promise = promise.then(function(i){
        return function(){
            doQuery(i);
        };
    }(i));
}

In modern Javascript engines you can also use the native Function.prototype.bind:

在现代Javascript引擎中,您还可以使用本机Function.prototype.bind:

var promise = doQuery(0);
for (var i = 1; i < 10; i++) {
    promise = promise.then(doQuery.bind(null, i));
}

#2


2  

You need to return each to-be-chained promise from the then callback, otherwise it will likely fail. To pass the right i value to each callback, see JavaScript closure inside loops – simple practical example.

您需要从当时的回调中返回每个待链接的承诺,否则它可能会失败。要将正确的i值传递给每个回调,请参阅循环内的JavaScript闭包 - 简单的实际示例。

var promise = doQuery(0);
for (var i=0; i<10; i++) (function(ii) {
    promise = promise.then(function() {
        return doQuery(ii);
    });
})(i);