Reputation: 13306
I was wondering today whether I could not simply support both callbacks and promises. Are there any downsides you can think of?
function fetchUser(data, success, error) {
var deferred = Q.defer();
var body = {
sharedSecret: config['apiSharedSecret'],
deviceAuth: data.deviceAuth,
username: data.username,
phoneNumber: data.phoneNumber
};
request.post({
url: url,
json: body,
}, function (err, httpResponse, body) {
if (err) {
deferred.reject(new Error(err));
if (typeof error === "function") {
error.call(this, err);
}
return;
}
if (typeof success === "function") {
success.call(this, body);
}
deferred.resolve(body);
});
return deferred.promise;
}
Upvotes: 0
Views: 120
Reputation: 8139
The downside is that it can make your code more complex. Also there is no situation where callbacks have any advantage over promises so there is no point in doing this. But if you want to do it you should probably write a function like this to avoid code duplication.
function defer(success, error) {
var deferred = Q.defer();
deferred.promise.then(success, error);
return deferred;
}
Upvotes: 1