Last active
September 10, 2016 10:09
-
-
Save kritollm/816b77e0537ff4a13d2f0ba7d1006952 to your computer and use it in GitHub Desktop.
Wrap node style callback functions so it returns 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
function promisewrapper(fn) { | |
return (...args) => { | |
return new Promise((resolve, reject) => { | |
args.push((error, body) => { | |
if (error) { | |
return reject(error); | |
} | |
return resolve(body); | |
}); | |
fn(...args); | |
}); | |
} | |
} | |
const fs = require('fs'); | |
const readFile = promisewrapper(fs.readFile); | |
//const writefile = promisewrapper(fs.writeFile); | |
readFile('JsonData/yourfile.json', 'utf8').then(console.log.bind(console)).catch(console.error.bind(console)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment