Skip to content

Instantly share code, notes, and snippets.

@otarim
Created September 10, 2015 05:02
Show Gist options
  • Select an option

  • Save otarim/e93a63b23a25e58e0ee6 to your computer and use it in GitHub Desktop.

Select an option

Save otarim/e93a63b23a25e58e0ee6 to your computer and use it in GitHub Desktop.
convert thunk into promise
var promiseAble = function(fn){
return new Promise(function(resolve,reject){
fn(function(err,data){
if(err){
reject(err)
}else{
if (arguments.length > 2) {
data = slice.call(arguments, 1)
}
resolve(data)
}
})
})
}
var _asyncFn = promiseAble(function(cb){
fs.readFile('./es.js',cb)
})
_asyncFn.then(function(data){
console.log(data)
},function(err){
console.log(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment