Skip to content

Instantly share code, notes, and snippets.

@schahriar
Created January 14, 2018 00:27
Show Gist options
  • Select an option

  • Save schahriar/9b85a8ee2c5ee85b5b5397ae712deeef to your computer and use it in GitHub Desktop.

Select an option

Save schahriar/9b85a8ee2c5ee85b5b5397ae712deeef to your computer and use it in GitHub Desktop.
Util#promisify #async-await #medium
const fs = require('fs');
const { promisify } = require('util');
// Function#bind as needed
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const transform = () => /* ... */;
const transformFileAsync = async (source, dest) => {
const data = await readFile(source);
// Do some transformation
const transformedData = tranform(data);
await writeFile(dest, transformedData);
};
transformFileAsync('x.js', 'y.js').then(...).catch(...);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment