jQuery , Promises/A +, , , .catch .then. /, .
jQuery . A. Then (.catch ) . "", , .
, , Array.prototype.reduce().
function getWebServiceResults() {
return callWebService().then(function(data) {
var myList;
try {
myList = JSON.parse(data);
}
catch (error) {
return $.Deferred().reject(error).promise();
}
var promise = myList.reduce(function(promise, item) {
return promise.then(function(arr) {
return callWebService(item).then(function(result) {
arr.push(result);
return arr;
}, function(jqXHR, textStatus, errorThrown) {
if(checkIfContinuable(errorThrown)) {
return $.when(arr);
} else {
return new Error(textStatus);
}
});
});
}, $.when([]))
return promise.then(null, failWithTerribleError);
});
}
:
function getWebServiceResults() {...}
.
,callWebService()
item
- myList
.- ,
checkIfContinuable()
. , errorThrown
, jqXHR textStatus.