I'm really new to promises (I'm using bluebird promises with expressjs). I like how they tidy up the code. However, I'm not quite sure how to use them in some scenarios. I'm not even sure I'm using them right in my example.
The issue I have is that I'd like to pass some truthy or falsy to ".then" part of the promise. I've commented the code below for further details.
Thank you in advance!
//data is an array of javascript objects
Promise.each(data, function(d){
delete d.offset;
if (d.driver_id == -1) d.driver_id = null;
Promise.try(function(){
return new Term().where({'date_of_driving': d.date_of_driving}).fetch()
})
.then(function(result){
d.updated_at = dater.formatDate(new Date(), true);
if (result !== null) {
//update
var res = result.toJSON();
return new Term({'id': res.id}).save(d);
} else {
//save
d.created_at = dater.formatDate(new Date(), true);
return new Term().save(d);
}
}).then(function(item){
//I need to know here if before I saved or updated Term record
//is there a way to pass a truthy or falsy variable?
})
})
.then(function(terms){
})
.catch(function(error){
callback(false);
});