Basic question.

50 views
Skip to first unread message

Kevin Burton

unread,
Apr 20, 2014, 12:11:59 AM4/20/14
to q-con...@googlegroups.com
I have about 20 standard node type methods ( function(args, callback)). Each of these functions require the previous to be completed before the next can correctly proceed. For sake of discussion I will name these functions a,b,c,d,e,f..... So because these functions do not block I have "callback hell", that looks like:

a(args, function(err) {
    if(err) {
    } else {
        b(args, function(err) {
            if(err) {
            } else {
                c(args, function(err) {
                    if(err) {
                    } else {
                        d(args, function(err) {
                            if(err) {
                            } else {
                                e(args, function(err) {
                                     if(err) {
                                     } else {
                                        f(args, function(err) {
                                             if(err) {
                                             } else {

Looking at the documentation I am not sure if I should use 'defer', 'denodeify', or 'ninvoke'. Like I said I am new to 'Q' and am just trying to glean what I can form the docs. Can it be as simple as:

a(args)
.then(b(args))
.then(c(args))
.then(d(args))
.then(e(args))
.then(f(args))
.error(err) {
    //handle error
}
.done();

Any experience with this simple scenario would be greatly appreciated and would jumpstart my understanding of Q and promises.

Thank you.

Stuart Knightley

unread,
Apr 21, 2014, 2:55:37 PM4/21/14
to q-con...@googlegroups.com
If you adapt a,b,c,d,e,f... to return promises, then it can indeed be as simple as .then().then()...

If you control a, b, c... you will use Q.defer inside them to create a new promise to return. If you don't control them (e.g. an existing callback based package) then you can use either denodify or ninvoke to create a promise returning function or invoke the function immediately and return a promise respectively.

You may be interested in dropping some of your code into http://stuk.github.io/promise-me/ to see what it looks like (note this just converts the *usage* from callback to promise, it doesn't make the changes to the functions themselves). 

Stuart

// ravi

unread,
Apr 21, 2014, 2:55:49 PM4/21/14
to q-con...@googlegroups.com
Hello Kevin,

note that when you do .then(b(args)) you are actually invoking b(args), not passing a reference to it. What might work in your case:

Q.nfcall(a, arg1, arg2, …)
.then(Q.denodify(b, arg1, arg2, ...))
.then(Q.denodify(c, arg1, arg2, …))
.fail
(
    function(err)
    {
        // handle error
    }
)
.done();

—ravi


Stuart Knightley

unread,
Apr 21, 2014, 2:58:50 PM4/21/14
to q-con...@googlegroups.com


On Saturday, 19 April 2014 21:11:59 UTC-7, Kevin Burton wrote:
a(args)
.then(b(args))
.then(c(args))
.then(d(args))
.then(e(args))
.then(f(args))
.error(err) {
    //handle error
}
.done();

Just rereading this, your code would look like this:

a(args)
.then(function (aResult) {
   return b(args);
})
.then(function (bResult) {
   return c(args);
})
// etc.
.catch(function (error) {
   // handle error
})
.done();

As you wrote it a, b, c... would all be called immediately, rather then when the previous promise was resolved.

// ravi

unread,
Apr 21, 2014, 3:04:46 PM4/21/14
to q-con...@googlegroups.com
On Apr 21, 2014, at 2:55 PM, // ravi <ravi-...@g8o.net> wrote:

note that when you do .then(b(args)) you are actually invoking b(args), not passing a reference to it. What might work in your case:

Q.nfcall(a, arg1, arg2, …)
.then(Q.denodify(b, arg1, arg2, ...))
.then(Q.denodify(c, arg1, arg2, …))


Note that in the above, if a() returns a value, that will be passed to b() as its last parameter/argument; similarly for b()’s return value passed to c(), etc.

—ravi


Reply all
Reply to author
Forward
0 new messages