承诺承诺

时间:2017-11-21 08:31:47

标签: javascript es6-promise

我有检查数组中值的函数

    const arr = [10,20,30];
    const check = (needle) => {
            return new Promise((resolve, reject) => {
                arr.includes(needle) ? resolve('err') : reject('err');
            })
        };

在自定义案例

中的工作原理如下
checkPermissionByRole(100)
            .then(val => console.log(val))
            .catch( err => console.log(err)) // gives 'err' as expected 

但我想做点像

const arr = [10,20,30];
const second = [40,50,60];

const check = (needle) => {
                return new Promise((resolve, reject) => {
                    arr.includes(needle) ? resolve('err') : // call check again but with another array to check in
                })
            };

如果一个promise给了我一个被拒绝的值,请使用相同的函数(和相同的参数)再次检查它,它给了我一个承诺,但是list / array(arr one)应该是second而不是arr。我以为我可以用携带的结构来做,如果没有,给我一些思考的面包

2 个答案:

答案 0 :(得分:3)

如果要检查两个数组中是否包含needle,您可以这样做:

(arr.includes(needle) || second.includes(needle)) ? resolve('err') : reject('err');

答案 1 :(得分:0)

试试这个:

const arr = [10,20,30];
const second = [40,50,60];

const check = (needle) => {
  return new Promise((resolve, reject) => {    
    arr.includes(needle) && resolve('err1') ||
    second.includes(needle) && resolve('err2') || reject('err');
  })
};