Skip to content

Instantly share code, notes, and snippets.

@yesvods
Last active November 19, 2018 23:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yesvods/175995171679f49a7962 to your computer and use it in GitHub Desktop.
Save yesvods/175995171679f49a7962 to your computer and use it in GitHub Desktop.
wrap nodejs fs#readFile to be a promise.
let fs = {
readFile: function(filename, cb){
setTimeout(()=>{
cb('hello')
}, 100)
}
}
let readFilePro = function(filename){
return new Promise(function(resolve, reject) {
fs.readFile(filename, (data, err) => {
if(err) reject(err);
resolve(data)
})
})
}
readFilePro('file').then(x => {
console.log(x)
})
@jsepulvedaco
Copy link

you have a problem there at line 10: the 'err' argument should go first in the callback function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment