Last active
October 31, 2017 20:50
-
-
Save SeanJM/a9cb0399c5fcd4fb2de711715a3dc3da to your computer and use it in GitHub Desktop.
A function which will turn node style callbacks into Promises.
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
module.exports = function promisify(fn) { | |
const args = []; | |
for (var i = 1, n = arguments.length; i < n; i++) { | |
args.push(arguments[i]); | |
} | |
return new Promise(function (resolve, reject) { | |
args.push(function (err) { | |
const args = []; | |
if (err) { | |
reject(err); | |
} else { | |
for (var i = 1, n = arguments.length; i < n; i++) { | |
args.push(arguments[i]); | |
} | |
resolve.apply(resolve, args); | |
} | |
}); | |
fn.apply(fn, args); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment