Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking out of a Q promise in Node.js when performing a callback?

Please excuse my newbishness with the concept of promises. I'm using the Q module in Node.js. I have a function that is meant to call a callback once it has performed all the necessary steps. The problem occurs when I want to call the callback function from within the Q promise.

My desired functionality is to be able to call the callback when I reach the final step, and no longer be within the chain of promises. Thus, the callback will be back to its original operation. However, as I have coded it, the callback gets called within the context of the promise. At this point, should the callback (say) throw an error, it gets caught by the error handler in this function, which is not what I 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 happening within the callback function causes "OHNOES!!!!!" to be printed....

like image 887
Zane Claes Avatar asked Jul 27 '12 23:07

Zane Claes


People also ask

How do I use Q Promise in node JS?

var Q = require('q'); // this is suppose, the async function I want to use promise for function async(cb) { setTimeout(function () { cb(); }, 5000); } async(function () { console. log('async called back'); }); How do I use Q and its . then here in my example?

What is the difference between callback and Promise in Nodejs?

To implement asynchronous code in JavaScript we use callback functions and promises. A callback function is passed as an argument to another function whereas Promise is something that is achieved or completed in the future.

Which is faster Promise or callback?

So from my findings i assure you ES6 promises are faster and recommended than old callbacks. I recommend to get a common understanding of JS event loop.

What are the advantages of using promises instead of callbacks?

They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events. In other words also, we may say that, promises are the ideal choice for handling multiple callbacks at the same time, thus avoiding the undesired callback hell situation.


1 Answers

There is a method, nodeify that will (optionally) break out of a promise chain and forward to a NodeJS-style continuation.

var updateDataStream = function(data, input, posts, stream, callback) {

    // Pack all the items up...
    return 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);
    })
    .nodeify(callback);

}

Note the added "return" at the beginning of the chain and the "nodeify(callback)" added at the end.

Your users need be none the wiser that you’re using Q at all…unless they leave off the callback, in which case they will get a promise instead.

like image 102
Kris Kowal Avatar answered Sep 29 '22 23:09

Kris Kowal