Skip to content

Instantly share code, notes, and snippets.

@domenic
Created February 9, 2012 20:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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

+1 for composability

@domenic
Copy link
Author

domenic commented Feb 9, 2012

@ryanwitt Are you on q-continuum? I started the discussion about it over there :)

@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