Created
April 6, 2016 11:23
-
-
Save paolobueno/7deccf12e876dacc9004136ef1f1fb24 to your computer and use it in GitHub Desktop.
Simple promisify
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
| /** | |
| * Simple promisifier for a function that expects a node-like callback | |
| * @param {Function} fn Function to promisify, should expect a node-like callback as the last parameter | |
| * @return {Function} the promisified wrapped version of the function | |
| */ | |
| module.exports = function promisify (fn) { | |
| return function () { | |
| var paramsArr = Array.prototype.slice.call(arguments); | |
| return new Promise(function (resolve, reject) { | |
| var callback = function (err, res) { | |
| if (err) {return reject(err);} | |
| resolve(res); | |
| }; | |
| paramsArr.push(callback); | |
| fn.apply(this, paramsArr); | |
| }); | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment