bonded
The bonded
module provides functions for wrapping Node.js style calls
function(arg_1, ..., function(err, result))
in Promise
objects.
Example - Wrap a standard Node.js asynchronous call within a Promise
var bonded = fs = fdMaybe fd; // create a Promise variant of fs.openfsopenMaybe = bonded; // use the function to get a Promised file descriptorfdMaybe = fs; // set the fd once the Promise is fulfilled or display errorfdMaybe ;
Example - Wrap within a Promise an asynchronous call which does not pass an error
var bonded = fs = existsEventually; // created a Promise variant of fs.existsfsexistsEventually = bonded; // use the function to get a Promised resultexistsEventually = fs; // display a message once the Promise is fullfilledexistsEventually;
this
context
Example - Binding var bonded = nano = "http://example.com:5984" // nano CouchDB client db = nanodb; // won't work; nano.db.insert requires 'this' to be bound to 'db'var insertMaybe = bonded; // you could do this, of coursevar insertMaybe = bonded; // you can also do this with the maybe functionvar insertMaybe = bonded; // use the function to insert a documentvar insertedMaybe = ; // handle success/failureinsertedMaybe;
Other Notes
- what if the callback passes multiple results? (the nano example glosses over
this detail;
insert
passes body and headers to callback)