3 ms·
I find the "Do not block the event loop" example terrible: const fs = require('fs'); fs.readFile('/file.txt', (err, data) => { // perform actions on fi
by kilburn 5y ago
I find the "Do not block the event loop" example terrible:
const fs = require('fs');
fs.readFile('/file.txt', (err, data) => {
// perform actions on file content
fs.unlink('/file.txt', (err) => {
if (err) throw err;
});
});
This comes right after a section about "use flat promise chains" and is also vulnerable to leaving file.txt behind if the "perform actions" part throws. Much better example imho:
const fs = require('fs/promises');
const data = await fs.readFile('/file.txt');
try {
// Do something with the data
} finally {
await fs.unlink('/file.txt');
}
- siproprio 5y agoYes! Thank you! This reminded example reminded me why I hated working with node!