Javascript 蓝鸟承诺系列控制流
我有很多承诺,我想按顺序但有条件地履行它们 例如,我有以下承诺:Javascript 蓝鸟承诺系列控制流,javascript,bluebird,Javascript,Bluebird,我有很多承诺,我想按顺序但有条件地履行它们 例如,我有以下承诺: getItems() getItemsSource() getItemsAlternativeSource() 我想做的是首先尝试getItems()。如果这是一个空值或抛出一个错误,我想记录该错误(如果是这种情况),然后尝试getItemsSource(),如上所述,如果它没有值或抛出一个错误,我想记录错误(如果是这种情况),然后尝试getItemsAlternativeSource() 我知道我可以有条件地这样做,在每个the
getItems()
getItemsSource()
getItemsAlternativeSource()
我想做的是首先尝试getItems()
。如果这是一个空值或抛出一个错误,我想记录该错误(如果是这种情况),然后尝试getItemsSource()
,如上所述,如果它没有值或抛出一个错误,我想记录错误(如果是这种情况),然后尝试getItemsAlternativeSource()
我知道我可以有条件地这样做,在每个then()
或catch()
中,但这似乎有点多余。有没有更好的方法来处理这种控制流
谢谢! 您可以使用空值作为
catch
处理程序的返回值:
getItems().catch(function(err) {
console.warn(err);
return null; // <==
}).then(function(items) {
if (items) return items;
else return getItemsSource().catch(function(err) {
console.warn(err);
return null; // <==
}).then(function(sourceitems) {
if (items) return items;
else return getItemsAlternativeSource().catch(function(err) {
console.warn(err);
throw new Error("items couldn't be fetched normally, from source, or from alternative source");
});
});
});
您可以使用空值作为
catch
处理程序的返回值:
getItems().catch(function(err) {
console.warn(err);
return null; // <==
}).then(function(items) {
if (items) return items;
else return getItemsSource().catch(function(err) {
console.warn(err);
return null; // <==
}).then(function(sourceitems) {
if (items) return items;
else return getItemsAlternativeSource().catch(function(err) {
console.warn(err);
throw new Error("items couldn't be fetched normally, from source, or from alternative source");
});
});
});
我建议您创建一个函数,它接受一个函数数组,该数组将调用数组中的每个函数,直到其中一个函数返回一些数据
function getFirstData(array) {
var index = 0;
function next() {
if (index < array.length) {
return array[index++]().then(function(data) {
// if we got an answer, return it as the resolve value
if (data) return data;
// otherwise, reject so we go to the next one
return Promise.reject(null);
}).catch(function(err) {
if (err) console.err(err);
return next();
});
} else {
// got to the end of the array without a value
throw new Error("No data found");
}
}
return Promise.resolve().then(next);
}
var fns = [getItem, getItemsSource, getItemsAlternativeSource];
getFirstData(fns).then(function(data) {
// got data here
}).catch(function(err) {
// no data found here
});
我建议您创建一个函数,它接受一个函数数组,该数组将调用数组中的每个函数,直到其中一个函数返回一些数据
function getFirstData(array) {
var index = 0;
function next() {
if (index < array.length) {
return array[index++]().then(function(data) {
// if we got an answer, return it as the resolve value
if (data) return data;
// otherwise, reject so we go to the next one
return Promise.reject(null);
}).catch(function(err) {
if (err) console.err(err);
return next();
});
} else {
// got to the end of the array without a value
throw new Error("No data found");
}
}
return Promise.resolve().then(next);
}
var fns = [getItem, getItemsSource, getItemsAlternativeSource];
getFirstData(fns).then(function(data) {
// got data here
}).catch(function(err) {
// no data found here
});