Skip to content

Instantly share code, notes, and snippets.

@kritollm
Last active September 10, 2016 10:09
Show Gist options
  • Save kritollm/816b77e0537ff4a13d2f0ba7d1006952 to your computer and use it in GitHub Desktop.
Save kritollm/816b77e0537ff4a13d2f0ba7d1006952 to your computer and use it in GitHub Desktop.
Wrap node style callback functions so it returns a promise.
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