Search code examples
javascriptes6-promise

Carrying through Promise


i've got function that checks value in array

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

and works like this in custom case

checkPermissionByRole(100)
            .then(val => console.log(val))
            .catch( err => console.log(err)) // gives 'err' as expected 

But i wanted to make something like

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

if a promise gives me a rejected value, check it again with the same function(and the same argument) that gives me a promise, but list / array (arr one) should be second instead of arr. Thought i could do it with a carrying construction, and if not, give me some bread for thinking


Solution

  • If you want to check if needle is contained in any of two arrays, you can do:

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