Skip to content

Instantly share code, notes, and snippets.

@dominykas
Created January 31, 2013 12:45
Show Gist options
  • Save dominykas/4682612 to your computer and use it in GitHub Desktop.
Save dominykas/4682612 to your computer and use it in GitHub Desktop.
obj.method(arg, options, function(err, value){
if (err) {
// handle or rethrow
}
doSomethingWith(value);
});
// alternative to promise-ify (with Q)
Q.ninvoke(obj, "method")
.then(function(value){
doSomethingWith(value)
})
.fail(function(err){
// handle or rethrow
})
.done();
// if you want to use promises inside your lib, you can also do this
obj.method = function(args, options, cb)
{
// do whatever you need to do
return Q.resolve(value).nodeify(cb);
}
// to use the above, one can go simple callback style, i.e. exactly as the very first example here
obj.method(args, options, function(e, v){ /* same as first example */ });
// or one can directly use the promises API like this:
obj.method(args, options)
.then(function(v){ /* do stuff */ })
.fail(function(e){ /* handle stuff */ })
.done();
// promises FTW!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment