Last active
March 25, 2022 12:45
-
-
Save rasolofonirina/85a6a001ac564cc049885a6e82c5a450 to your computer and use it in GitHub Desktop.
Dangers of mixing blocking and non-blocking code
This file contains 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
// Mix | |
const fs = require('fs') | |
fs.readFile('/file.txt', (err, data) => { | |
if (err) throw err | |
console.log(data) | |
}) | |
fs.unlinkSync('/file.txt') | |
// Better way | |
const fs = require('fs') | |
fs.readFile('/file.txt', (readFileErr, data) => { | |
if (readFileErr) throw readFileErr | |
console.log(data) | |
fs.unlink('/file.txt', (unlinkErr) => { | |
if (unlinkErr) throw unlinkErr | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment