DEV Community

Discussion on: Refactoring node.js (Part 1)

Collapse
 
jsardev profile image
Jakub Sarnowski
const readFilePromise = util.promisify(fs.readFile);

const readFile = async (path) => {
    return await readFilePromise(path);
}

Here, the readFilePromise is exactly the same as readFile. You could just do: const readFile = util.promisify(fs.readFile);

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thanks for the suggestion! I was actually aware of that, but I decided to wrap readFilePromise in a function anyway to illustrate how, after using promisify, we can await the "promisified" function within an async function. I couldn't come up with a better example, but I'll try to think of one to avoid any confusion.

Collapse
 
jsardev profile image
Jakub Sarnowski

I was thinking that this might be the case, but I've decided to comment it out, just in case 😄 Great article!

Collapse
 
paulasantamaria profile image
Paula Santamaría

It's 5am, I couldn't sleep and I came up with a better example that made more sense 😂. I just replaced the old one.