Created
January 5, 2017 12:43
-
-
Save xiekw2010/b2dcd96f547db92bb142dc2868e0ea83 to your computer and use it in GitHub Desktop.
thunk 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
/** | |
* | |
* @param fn | |
* @returns {Promise} | |
* eg: thunkToPromise(fs.readdir, path).then() | |
*/ | |
function thunkToPromise(fn) { | |
var ctx = this | |
var args = Array.prototype.slice.call(arguments, 1) | |
return new Promise(function (resolve, reject) { | |
args.push(function (err, res) { | |
if (err) reject(err) | |
resolve(res) | |
}) | |
fn.apply(ctx, args) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment