luciotato / waitfor

Sequential programming for node.js, end of callback hell / pyramid of doom
MIT License
531 stars 29 forks source link

Changed "resumeCallback", because when you have a function with a callba... #20

Closed ryuken closed 10 years ago

ryuken commented 10 years ago

Changed "resumeCallback", because when you have a function with a callback parameter you currently only get the second parameter, but when the callback has more than two parameters there isn't any option to get them.

Example: connection.query(sql, data, function(err, rows, columns){ });

function query has a callback with parameters err, rows and columns.

Currently: try { var result = wait.forMethod(connection, "query", options.sql, options.params); // << result = rows } catch(err) { console.log(err); } You can catch the first param "err" and get the second param "rows" as the result, but there is not option to get the columns parameter.

With my fix, you get every param after the first param try { var result = wait.forMethod(connection, "query", options.sql, options.params); // << result = [rows, columns] } catch(err) { console.log(err); }

luciotato commented 10 years ago

the problem with the change is that now "data" is an array. - that's a breaking change.

Wait.for works only with standardized callbacks, a standardized callback always returns (err,data) in that order.

A better solution for the sql.query and any other non-standard callbacks is to create a wrapper function standardizing the callback, e.g.:

     connection.prototype.q = function(sql, params, stdCallback){ 
                 this.query(sql,params, function(err,rows,columns){ 
                                     return stdCallback(err,{rows:rows,columns:columns}); 
                             });
     }

all wrappers are simple functions standardizing the non-standard callback.

 try {
 var result = wait.forMethod(connection, "q", options.sql, options.params); 
 console.log(result.rows);
 console.log(result.columns);
 } 
    catch(err) {
       console.log(err);
    }
ryuken commented 10 years ago

Thanks I didn't think of using a wrapper function, but now its clear :D