First of all I'd like to ask if this is a good solution for working with promises.
app.get('/sample', function(req, res) {
var promiseFlow = {
step1: function() {
return modelPromise1();
},
step2: function(result) {
if(result) {
// I would like to stop the promise chain here
res.send(result);
} else {
return modelPromise2();
}
},
step3: function(result) {
if(result) {
res.send(result);
} else {
// This will be sent always
res.send(false);
}
}
}
promiseFlow.step1()
.then(promiseFlow.step2)
.then(promiseFlow.step3)
.catch(function(error) {
console.log(error);
});
});
User model example:
_self.get_user_info = function(_guid) {
var deferred = modules.Q.defer();
modules.system.db.connect(deferred, function(connection) {
var query = modules.qb.select().from('users')
.field('_guid')
.field('username')
.where('_guid = ?', _guid);
connection.query(query.toString(), function(error, result) {
connection.release();
if(error) {
deferred.reject(error);
return;
}
if(result.length) {
deferred.resolve(result);
} else {
deferred.reject(null);
}
});
});
return deferred.promise;
}
My main problem is with this that I don't know how to break the chain to not execute the next step. The res.send(false) will happen anyway in step3. Can this thing work or I should choose an other pattern for this.
Thanks!