turbo
turbo

Reputation: 589

promise in Nodejs api design

I am just started using Q library for the promises. I have this common pattern in most of the api's.

userAction1(params)
.then(function(result){
    response.json(result);
},function(err){
    response.json(err);
});

userAction2(params)
.then(function(result){
    response.json(result);
},function(err){
    response.json(err);
});

I want to move the last part to some common method and call it instead of repeating it. Little confused about what should be signature of that function. Anyone went through this problem before?

Upvotes: 0

Views: 99

Answers (1)

Ben Fortune
Ben Fortune

Reputation: 32117

By the "last part", I assume you mean the error handling function.

You can shorten it somewhat by passing the method as a reference.

If you're not doing anything else in your function, it would look like this.

userAction1(params)
    .then(response.json, response.json);

Upvotes: 1

Related Questions