Created
February 5, 2016 03:15
-
-
Save patrixr/6b467c7f14e5e87045a9 to your computer and use it in GitHub Desktop.
Promisify: from callback to promise
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
function promisify(func, scope) { | |
return function () { | |
var args = [].slice.call(arguments); | |
var deferred = Promise.defer(); | |
args.push(function () { | |
if (arguments[0]) { | |
// There was an error | |
deferred.reject(arguments[0]); | |
} else { | |
// Success | |
deferred.resolve([].slice.call(arguments, 1)); | |
} | |
}); | |
func.apply(scope || null, args); | |
return deferred.promise; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment