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.

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top