public
Last active

Q.bind

  • Download Gist
q-bind.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// 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`.

+1 for composability

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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.