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.