1

I have a code in my controller, that works on an http request, to save a post that user added in the front-end. So I do something like, Model.create(). Now what happens is on the call backs I want to return whether the post was successfully added or not, so that my front-end could show appropriate messages to the user. But the create method works asynchronously. Following is a sample code:

create: function(req, res){

    Activity.create(req.body).done(
            function(err, persistedActivity) {

                    // Error handling
                    if (err) {

                        return res.json({success: false});
                        // The Activity could not be created!
                    } else {

                        return res.json({success : true});
                    }
            }
    );

}

But this code doesn't work because Activity.create() method is async. I have also read about it in other questions or posts. Nothing suggests the exact implementation I am looking for, but it does tell me "don't fight the asynchronous nature of nodejs. It will only hurt later.", and trust me, like everyone else, I don;t wanna get hurt. I essentially need suggestions on how to handle this.

Sambhav Sharma
  • 5,741
  • 9
  • 53
  • 95
  • possible duplicate of [How to return value from an asynchronous callback function?](http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) – Qantas 94 Heavy May 17 '14 at 10:32
  • Nope, couldn't get it there. It doesn't give me a solution, have gone through it. – Sambhav Sharma May 17 '14 at 10:40
  • Okay, could you return a promise from the function instead? – Qantas 94 Heavy May 17 '14 at 10:42
  • I am looking for suggestions here, and if it is a suggestion, I will look into promises and revert back.. – Sambhav Sharma May 17 '14 at 10:46
  • What's the problem here, doesn't the code work - what response do you get? The response for a request does not need to be created synchronously, and calling `res.json()` from an async callback is perfectly fine. – Bergi May 19 '14 at 01:30
  • Yes, I actually found out that it works perfectly fine, the problem with it was, that I actually had some part of code out of the call back, and since I was new to JS, I did not know much about how to nest callbacks. Although, the promises approach is also pretty good to evaluate.. – Sambhav Sharma May 20 '14 at 06:49

1 Answers1

2

If you wanted to try this with Sails promises, it would look like this:

create: function(req, res){
    Activity.create(req.body)
    .then(function(persistedActivity) {
        return res.json({success : true});
    })
    .fail(function(err){
        return res.json({success: false});
        // The Activity could not be created!
    })
}

I'm not as familiar using SailsJS without promises, but you might want to try using exec instead of done when chaining your create call? I know I've used exec, but I've never used done in that way.

-- Update: It seems that done() is deprecated in favor of exec() so I would definitely try that if you are using 10.x reference

Jason Kulatunga
  • 5,814
  • 1
  • 26
  • 50