Frage

How does futures handle a callback that has more than one argument? This is critical to just about every use I could have for futures. The github example shows it only handleing one argument.

The example from the Github Readme is

var fileNames = readdir('.').wait();

But what about something a mysql call like

client.query("select * from employees", function(err, results, fields) {
    // callback function returns employees array
    callback(results);
});

How would I get those three (err, results, and fields) using the futures wait() method?

Edit

Experimentation has taught me that the first argument of the callback, in this case err, is always treated as an error and is thrown if the value is truthy. The second argument is assigned. Any further arguments are ignored as best as I can tell.

War es hilfreich?

Lösung

Create a wrapper for client.query, returning one data parameter with both results.

 client.prototype.query2 = function ( sql, callback ) {
      this.query(sql,
              function(err, results, fields) {
                  var data={result:result, fields:fields};
                  callback(err,data);
              }
 };

I've recently created simpler abstraction than 'futures', also based on Fibers. I haven't tested my code with a real world DB example yet. Please try it and help me test it if you can: https://github.com/luciotato/waitfor

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top