0

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);
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
uglycode
  • 3,022
  • 6
  • 29
  • 55

2 Answers2

1

I'm not even sure I'm using them right in my example.

It looks like you've forgotten to return a promise from your each() callback. Also, I'm not sure whether you need that Promise.try wrapper, I would think using the promise returned by .fetch() directly should be pretty safe.

The issue I have is that I'd like to pass some truthy or falsy to ".then" part of the promise.

I think the easiest way here is to nest the then calls, and put the callbacks right for the respective .save() invocation.

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) {
        var res = result.toJSON();
        return new Term({'id': res.id}).save(d) // update
        .then(function(item) {
            // here you know that before you updated the Term record
        });
    } else {
        d.created_at = dater.formatDate(new Date(), true);
        return new Term().save(d) // save
        .then(function(item) {
            // here you know that before you saved the Term record
        });
    }
})

Basically, if your problem boils down to accessing the result !== null expression, see How do I access previous promise results in a .then() chain? for other approaches.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks, that's definitely one way of solving things. I was actually hoping to use as much chaining as possible and I came to this solution. Could you please examine it and comment if there are some issues with how I approach things? http://www.codeshare.io/QErX0 Thank you! – uglycode Mar 08 '15 at 14:46
  • Yeah, that'll work as well. Notice that the `return Promise.resolve();` can be simply omitted. However, I don't see any reason to chain here, as you *want* control flow branching which is achieved via nesting blocks. Have a look at my update on your code share (it doesn't have versioning, does it?) – Bergi Mar 08 '15 at 14:55
  • 1
    Bergi thank you for your explanation again! This is wonderful! I understand promises now a little bit better. – uglycode Mar 08 '15 at 18:03
0

It's quite easily actually. One of the ways is to add the state to the surrounding function (which in your case I think would suit your needs perfectly).

//data is an array of javascript objects
Promise.each(data, function(d){
    var updated; // state var
    delete d.offset;
    if (d.driver_id == -1) d.driver_id = null;  

    return 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
            updated = true;
            var res = result.toJSON();
            return new Term({'id': res.id}).save(d);
        } else {
            updated = false;
            //save
            d.created_at = dater.formatDate(new Date(), true);
            return new Term().save(d);               
        }
    }).then(function(item){
            // Here, you can use the updated variable.
    })
})
.then(function(terms){

})
.catch(function(error){
    callback(false);
});
Alxandr
  • 12,345
  • 10
  • 59
  • 95
  • A little explanation of what you did would make this a better answer. As it is now, we're left to try to compare this code to the original to figure out what you did. – jfriend00 Mar 08 '15 at 17:47