Skip to content

Instantly share code, notes, and snippets.

@domenic
Created February 9, 2012 20:20
Show Gist options
  • Save domenic/1782808 to your computer and use it in GitHub Desktop.
Save domenic/1782808 to your computer and use it in GitHub Desktop.
Q.bind
// From discussion at https://groups.google.com/d/msg/q-continuum/68ro20aB358/7kUCAF19qqAJ
// Naive version
function fAsync(a, b) {
if (a) {
return Q.reject(new Error());
}
if (b) {
return Q.resolve(42);
}
return Q.resolve();
}
// With `Q.call`
function fAsync(a, b) {
return Q.call(function f(a, b) {
if (a) {
throw new Error();
}
if (b) {
return 42;
}
}, null, a, b);
}
// With `Q.bind`
var fAsync = Q.bind(function f(a, b) {
if (a) {
throw new Error();
}
if (b) {
return 42;
}
});
// Could also pass context, bound args as with `Function.prototype.bind`.
@ryanwitt
Copy link

ryanwitt commented Feb 9, 2012

yes. it's just that i'm anti-social

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment