Skip to content

Instantly share code, notes, and snippets.

@xiekw2010
Created January 5, 2017 12:43
Show Gist options
  • Save xiekw2010/b2dcd96f547db92bb142dc2868e0ea83 to your computer and use it in GitHub Desktop.
Save xiekw2010/b2dcd96f547db92bb142dc2868e0ea83 to your computer and use it in GitHub Desktop.
thunk to promise
/**
*
* @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