Skip to content

Instantly share code, notes, and snippets.

@patrixr
Created February 5, 2016 03:15
Show Gist options
  • Save patrixr/6b467c7f14e5e87045a9 to your computer and use it in GitHub Desktop.
Save patrixr/6b467c7f14e5e87045a9 to your computer and use it in GitHub Desktop.
Promisify: from callback to promise
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