Created
October 31, 2012 22:22
-
-
Save boxxxie/3990342 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
exports.ninvoke = ninvoke; | |
function ninvoke(object, name /*, ...args*/) { | |
var args = array_slice(arguments, 2); | |
return napply(object[name], object, args); | |
} | |
exports.ncall = ncall; | |
function ncall(callback, thisp /*, ...args*/) { | |
var args = array_slice(arguments, 2); | |
return napply(callback, thisp, args); | |
} | |
exports.nbind = nbind; | |
function nbind(callback /* thisp, ...args*/) { | |
if (arguments.length > 1) { | |
var thisp = arguments[1]; | |
var args = array_slice(arguments, 2); | |
var originalCallback = callback; | |
callback = function () { | |
var combinedArgs = args.concat(array_slice(arguments)); | |
return originalCallback.apply(thisp, combinedArgs); | |
}; | |
} | |
return function () { | |
var deferred = defer(); | |
var args = array_slice(arguments); | |
// add a continuation that resolves the promise | |
args.push(deferred.makeNodeResolver()); | |
// trap exceptions thrown by the callback | |
fapply(callback, args) | |
.fail(deferred.reject); | |
return deferred.promise; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment