Created
January 31, 2013 12:45
-
-
Save dominykas/4682612 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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