Created
March 9, 2016 10:43
-
-
Save TheDeveloper/1312220bfbd27b742628 to your computer and use it in GitHub Desktop.
A little function to convert a callback-style function to Promises
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
// usage: promisify(function([args..., cb]) { /* do stuff with args then cb */ })() | |
exports.promisify = function(fn) { | |
return function(...args) { | |
return new Promise((resolve, reject) => { | |
fn(...args, (err, result) => { | |
if (err) return reject(err); | |
resolve(result); | |
}); | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment