0

I want to skip all the .then after a particular .then. But this particular .then will need to return a data to the caller.

function test() {    
    return User.findOne({ where: potentialUser })
        .then(function(user) { // level-1
            if(!user) {
                return { // data-1
                    status: 404,
                    json: { message: 'Not a valid user.' }
                };
            } else {
                return anyValidPromise;
            }
        })
        .then(function(isMatch) { // level-2

        })
        .then(function(val) { // level-3

        })
        .then(function(val) {

        })
        .then(function(val) {

        })
        .catch(function(error) {
            console.log(error);
            return {
                status: 500,
                json: { message: 'There was an error!' }
            };
        });
}

Now according to the above example level-1 must return data-1 to the caller of test. And all the .then from level-2 will not be executed.

Is it possible to achieve this?

If all the .then are executed then the caller is getting the data which is being returned at the last level of .then.

Jayadratha Mondal
  • 759
  • 1
  • 10
  • 21

1 Answers1

1

One of Promises advantages is "flattening" of the pyramid of doom

But sometimes, the required logic requires a certain level of "nesting"

This is one such case

function test() {    
    return User.findOne({ where: potentialUser })
    .then(function(user) { // level-1
        if(!user) {
            return { // data-1
                status: 404,
                json: { message: 'Not a valid user.' }
            };
        } else {
            return anyValidPromise
            .then(function(isMatch) { // level-2

            })
            .then(function(val) { // level-3

            })
            .then(function(val) {

            })
            .then(function(val) {

            })
        }
    )
    .catch(function(error) {
        console.log(error);
        return {
            status: 500,
            json: { message: 'There was an error!' }
        };
    });
}

Refactoring, you could do something like (I don't really like this by the way)

function test() {    
    let level4 = bar => {
        if(bar) {
            return {};
        } else {
            return {};
        }
    }
    let level3 = foo => {
        if(foo) {
            return {};
        } else {
            return level4(bar);
        }
    }
    let level2 = isMatch => {
        if(isMatch) {
            return {};
        } else {
            return level3(foo);
        }
    }
    let level1 = user => {
        if(!user) {
            return { // data-1
                status: 404,
                json: { message: 'Not a valid user.' }
            };
        } else {
            return anyValidPromise.then(level2)
        }
    };
    return User.findOne({ where: potentialUser })
    .then(level1)
    .catch(function(error) {
        console.log(error);
        return {
            status: 500,
            json: { message: 'There was an error!' }
        };
    });
}

Not sure if that is at all helpful though, your original code is "sparse" :p

Another possibility is to abuse the promise rejection chain for your own nefarious means

function test() {    
    return User.findOne({ where: potentialUser })
    .then(function(user) { // level-1
        if(!user) {
            throw { // data-1
                status: 404,
                json: { message: 'Not a valid user.' },
                skip: true
            };
        } else {
            return anyValidPromise;
        }
    })
    .then(function(isMatch) { // level-2
        if (youWantToSkipFromHere) {
            throw {
                status: ???
                json: ???
                skip: true
            };
        }
    })
    // etc etc
    .then(function(val) { // level-3
        if (youWantToSkipFromHere) {
            throw {
                status: ???
                json: ???
                skip: true
            };
        }
    })
    .catch(function(error) {
        if (error.skip) {
            return error;
        }
        console.log(error);
        return {
            status: 500,
            json: { message: 'There was an error!' }
        };
    });
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87