I used to have some node.js
implementation done using callbacks
but I am now refactoring my code to use Promises
ins
I see only two problems.
If you want to explicitly return a rejected promise with a value, you should do that with Q.reject.
Calling .done() on promise means that the promise ends there. It cannot be chained further.
So, your code would look like this
exports.update = function (id, template) {
if (!_isValid(template)) {
return Q.reject(new Error('Invalid data', Error.INVALID_DATA));
}
return _update(id, template);
};
Now, the update
function just returns a promise always. Its up to the callers to attach the success or failure handlers to it.