Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 蓝鸟承诺系列控制流_Javascript_Bluebird - Fatal编程技术网

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
});