Last active
November 19, 2018 23:25
-
-
Save yesvods/175995171679f49a7962 to your computer and use it in GitHub Desktop.
wrap nodejs fs#readFile to be a 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
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) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you have a problem there at line 10: the 'err' argument should go first in the callback function.