[英]How to account for errors in the ajax calls when using alternative promises
在构建将根据用户位置坐标提供天气数据的应用程序时,我已经开发了以下代码(也可在此代码库中找到:http: //codepen.io/PiotrBerebecki/pen/QNVEbP)。
用户的位置将通过getCoordinatesMethod1()
函数传递。 但是,如果失败,则该位置应由getCoordinatesMethod2()
函数传递。
如何将getCoordinatesMethod2()
添加到promises链(在底部),以便仅在getCoordinatesMethod1()
无法提供位置时使用?
另外,为了使应用程序更强大,将来我可能会添加其他方法来获取位置数据,因此链逻辑也应该能够容纳该数据。
// The first method to get user location coordinates.
function getCoordinatesMethod1() {
return $.ajax('http://wwww.example.com/coordinates-method-1.json');
}
// The second method to get user location coordinates.
// This should only be used if method 1 fails to deliver coordinates.
function getCoordinatesMethod2() {
return $.ajax('http://wwww.example.com/coordinates-method-2.json');
}
// Function which provides weather data depending on user coordinates.
function getCurrentWeather(latitude, longitude) {
return $.ajax('http://www.example.com/lat=' + latitude + '&lon=' + longitude + 'weather.json');
}
// Promises chain
getUserCoordinates1().then(function(locationData) {
return getCurrentWeather(locationData);
}).then(function(weatherData) {
return showCurrentWeather(weatherData);
})
您可以创建一个调用第一个方法的函数,如果返回的承诺被拒绝,则调用第二个方法:
function getCoordinateBoth() {
return getCoordinatesMethod1().then(null, getCoordinatesMethod2);
}
如果实际上有参数传递给这些函数,则可以像这样传递这些参数:
function getCoordinateBoth(/* args here */) {
var args = Array.prototype.slice.call(arguments);
return getCoordinatesMethod1.apply(null, args).then(null, function() {
return getCoordinatesMethod2.apply(null, args)
});
}
您甚至可以将这些函数放在一个数组中,因此它可以无限扩展:
// Put list of functions to be call as a chain until one succeeds
// You can just add more functions here as long as each returns a promise
// and expects the same arguments
var getCoordFunctions = [getCoordinatesMethod1, getCoordinateMethod2, getCoordinateMethod3];
function getCoordinateChain(/* args here */) {
var args = Array.prototype.slice.call(arguments);
var fnArray = getCoordFunctions.slice(0);
var firstFn = fnArray.shift();
fnArray.reduce(function(p, fn) {
return p.then(null, function() {
return fn.apply(null, args);
});
}, firstFn.apply(null, arguments));
}
您甚至可以使它成为通用的Promise链功能:
function chainUntilResolve(array /* other args here */) {
var args = Array.prototype.slice.call(arguments);
// toss first arg
args.shift();
// make a copy of array of functions
var fnArray = array.slice(0);
// remove first function from the array
var firstFn = fnArray.shift();
fnArray.reduce(function(p, fn) {
return p.then(null, function() {
return fn.apply(null, args);
});
}, firstFn.apply(null, arguments));
}
var getCoordFunctions = [getCoordinatesMethod1, getCoordinateMethod2, getCoordinateMethod3];
chainUntilResolve(getCoordFunctions, arg1, arg2).then(function(data) {
// process data here
}, function(err) {
// error here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.