Please excuse my newbie with the concept of promises. I am using the Q module in Node.js. I have a function that is designed to call a callback after completing all the necessary steps. The problem occurs when I want to call a callback function from a Q promise.
My desired functionality is to be able to call a callback when I get to the last step and will no longer be in the promises chain. Thus, the callback will return to the original operation. However, as I encoded it, the callback is called in the context of the promise. At this point, if the callback (say) throws an error, it falls into the error handler in this function, which I donโt want!
var updateDataStream = function(data, input, posts, stream, callback) { // Pack all the items up... Q.ncall(data._packStream, data, posts, stream) // Upsert the cache into the database .then(function(){ return Q.ncall(data.upsert, data); }) // buffer the new input .then(function(res){ return Q.ncall(data.buffer, data, input); }) .then(function(final){ callback(null, final); }) .fail(function(err){ console.log('OHNOES!!!!!!!',err); }).end(); }
In this context, an error that occurs in the callback function calls "OHNOES !!!!!" for print....
source share