$.when 是否以与数组中相同的顺序解析延迟数组?
Does $.when resolve array of deferreds in the same order as they were in the array?
我有一个数组,其中包含未知数量的延迟,我正尝试使用 $.when 来解决这些问题。当所有的 deferreds 都被解析后,'.then' 函数会按照我在数组中输入 then 的相同顺序返回已解析的 deferreds 吗?
伪代码示例:
var deferreds = [];
for(0,1,2,...,x) {
var def = JsonRpc(...);
deferreds.push(def);
}
$.when.apply($, deferreds).then(function () {
console.log(arguments.length); //outputs a number >= 0
for(0,1,2,...,x) {
console.log("Defered: ", arguments[0,1,2,...,x])
}
});
deferreds[0] 是否等于 arguments[0],deferreds[x] 是否等于 arguments[x]?
提前感谢所有回复!
//埃德文
根据文档:
The arguments passed to the doneCallbacks provide the resolved values
for each of the Deferreds, and matches the order the Deferreds were
passed to jQuery.when(). For example:
var d1 = $.Deferred();
var d2 = $.Deferred();
$.when( d1, d2 ).done(function ( v1, v2 ) {
console.log( v1 ); // "Fish"
console.log( v2 ); // "Pizza"
});
d1.resolve( "Fish" );
d2.resolve( "Pizza" );
一个小例子:
var deferreds = [];
for (var i = 0; i < 3; i++) {
var def = $.Deferred();
deferreds.push(def);
}
$.when.apply($, deferreds).then(function() {
console.log(arguments.length); //outputs a number >= 0
for (var i = 0; i < 3; i++) {
console.log("Defered: ", arguments[i]);
}
});
deferreds[1].resolve("Fish2");
deferreds[2].resolve("Fish3");
deferreds[0].resolve("Fish1");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
每个 question/statement 一次拿一个 ...
Does $.when resolve array of deferreds in the same order as they were in the array?
没有。 $.when()
不解析传递给它的 deferreds/promises。它 聚合 deferreds/promises,其中每一个都通过其他方式解决(解决或拒绝)。这里,"aggregate" 表示接受一组承诺,return 表示接受一个承诺。
I have an array of an unknown number of deferreds that I'm trying to resolve with $.when.
同样,$.when()
没有解析传递给它的 deferreds/promises。
When all the deferreds have been resolved, will the '.then' function give me back the resolved deferreds in the same order as I entered then in the array?
不,.then()
不会退回延期、已解决或其他情况。但是,是的,它的成功回调函数的参数(如果它触发)将与原始数组一致。
Will deferreds[0] be equal to arguments[0] and deferreds[x] be equal to arguments[x]?
不,它们不会相等,但它们会对应,逐个索引。每个参数将是原始数组中相应的deferred/promise传递的结果。
我有一个数组,其中包含未知数量的延迟,我正尝试使用 $.when 来解决这些问题。当所有的 deferreds 都被解析后,'.then' 函数会按照我在数组中输入 then 的相同顺序返回已解析的 deferreds 吗?
伪代码示例:
var deferreds = [];
for(0,1,2,...,x) {
var def = JsonRpc(...);
deferreds.push(def);
}
$.when.apply($, deferreds).then(function () {
console.log(arguments.length); //outputs a number >= 0
for(0,1,2,...,x) {
console.log("Defered: ", arguments[0,1,2,...,x])
}
});
deferreds[0] 是否等于 arguments[0],deferreds[x] 是否等于 arguments[x]?
提前感谢所有回复! //埃德文
根据文档:
The arguments passed to the doneCallbacks provide the resolved values for each of the Deferreds, and matches the order the Deferreds were passed to jQuery.when(). For example:
var d1 = $.Deferred();
var d2 = $.Deferred();
$.when( d1, d2 ).done(function ( v1, v2 ) {
console.log( v1 ); // "Fish"
console.log( v2 ); // "Pizza"
});
d1.resolve( "Fish" );
d2.resolve( "Pizza" );
一个小例子:
var deferreds = [];
for (var i = 0; i < 3; i++) {
var def = $.Deferred();
deferreds.push(def);
}
$.when.apply($, deferreds).then(function() {
console.log(arguments.length); //outputs a number >= 0
for (var i = 0; i < 3; i++) {
console.log("Defered: ", arguments[i]);
}
});
deferreds[1].resolve("Fish2");
deferreds[2].resolve("Fish3");
deferreds[0].resolve("Fish1");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
每个 question/statement 一次拿一个 ...
Does $.when resolve array of deferreds in the same order as they were in the array?
没有。 $.when()
不解析传递给它的 deferreds/promises。它 聚合 deferreds/promises,其中每一个都通过其他方式解决(解决或拒绝)。这里,"aggregate" 表示接受一组承诺,return 表示接受一个承诺。
I have an array of an unknown number of deferreds that I'm trying to resolve with $.when.
同样,$.when()
没有解析传递给它的 deferreds/promises。
When all the deferreds have been resolved, will the '.then' function give me back the resolved deferreds in the same order as I entered then in the array?
不,.then()
不会退回延期、已解决或其他情况。但是,是的,它的成功回调函数的参数(如果它触发)将与原始数组一致。
Will deferreds[0] be equal to arguments[0] and deferreds[x] be equal to arguments[x]?
不,它们不会相等,但它们会对应,逐个索引。每个参数将是原始数组中相应的deferred/promise传递的结果。