Created
January 14, 2018 00:27
-
-
Save schahriar/9b85a8ee2c5ee85b5b5397ae712deeef to your computer and use it in GitHub Desktop.
Util#promisify #async-await #medium
This file contains hidden or 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
| 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