Динамическая цепочка обещаний AJAX с jQuery
Мои вызовы AJAX встроены в цикл for. Они должны быть в порядке (синхронно). Как связать их с jQuery?
var array = ['One', 'Two', 'Three'];
var arrayLength = array.length;
for (var arrayCounter = 0; arrayCounter < arrayLength; arrayCounter++) {
var id = array[arrayCounter];
getData(id);
function getData(id) {
$.ajax({
url: 'http://example.com/' + id,
dataType: 'jsonp',
success: function(d) {
var response = d;
console.log(d);
},
error: function() {
alert("ERROR");
}
});
}
}
Ответы
Ответ 1
Решение с использованием for
:
var array = ['One', 'Two', 'Three'];
var id = array[0];
var data = getData(id);
for (var i = 1; i < array.length; i++) {
// Or only the last "i" will be used
(function (i) {
data = data.then(function() {
return getData(array[i]);
});
}(i));
}
// Also, see how better the getData can be.
function getData(id) {
return $.ajax({
url: 'http://example.com/' + id,
dataType: 'jsonp',
}).done(function(d) {
var response = d;
console.log(d);
}).fail(function() {
alert('ERROR');
});
}
Кстати, если вы использовали правильную библиотеку promises, такую как bluebird
, вы должны использовать следующий код:
var array = ['One', 'Two', 'Three'];
Promise.reduce(array, function(data, id) {
return data.promise.then(function(result) {
return { promise: getData(id), results: data.results.push(result) };
});
}, []).then(function(data) {
console.log(data.results); // yay!
});
function getData(id) {
return Promise.cast($.ajax({
url: 'http://example.com/' + id,
dataType: 'jsonp',
}).done(function(d) {
var response = d;
console.log(d);
}).fail(function() {
alert('ERROR');
}));
}
Как вы можете видеть, проще читать/писать.
Ответ 2
В большинстве библиотек с обещаниями встроена функция jQuery? Не так повезло:
Во-первых, ваша функция должна вернуть обещание:
function getData(id) {
return $.ajax({ // note the return
url: 'http://example.com/'+id,
dataType: 'jsonp',
success: function (d) {
console.log(d);
},
error: function () {
alert("ERROR")
}
});
}
Теперь вы связываете их в цикле с помощью вызовов .then
. Обратите внимание, что .then
будет выполняться только после выполнения предыдущего обещания. Таким образом, все они будут вести по порядку, один за другим.
var array = ['One', 'Two', 'Three'];
var p = $.when(1); // empty promise
array.forEach(function(el){
p = p.then(function(){
return getData(el);;
});
});
Все функции будут запускаться один за другим. Что осталось? Возвращаемое значение. Текущая реализация отбрасывает возвращаемое значение. Мы можем поместить возвращаемые значения в массив, например:
var array = ['One', 'Two', 'Three'];
var p = $.when(1); // empty promise
var results = [];
array.forEach(function(el,index){
p = p.then(function(){
return getData(el);
}).then(function(data){
results[index] = data; // save the data
});
});
p.then(function(){
// all promises done, results contains the return values
});
Зачем останавливаться, пусть делает его приятнее:) Весь ваш код можно сократить до
["One","Two","Three"].map(makeUrl).map($.get).reduce(function(prev,cur,idx){
return prev.then(cur).then(function(data){ results[idx] = data; });
},$.when(1)).then(function(){
// results contains all responses here, all requests are sync
});
function makeUrl(id){
return "http://example.com"+id+"?callback=?";
}