Created
February 9, 2012 20:20
-
-
Save domenic/1782808 to your computer and use it in GitHub Desktop.
Q.bind
This file contains 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
// 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 Are you on q-continuum? I started the discussion about it over there :)
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
+1 for composability