Skip to content

Instantly share code, notes, and snippets.

@rasolofonirina
Last active March 25, 2022 12:45
Show Gist options
  • Save rasolofonirina/85a6a001ac564cc049885a6e82c5a450 to your computer and use it in GitHub Desktop.
Save rasolofonirina/85a6a001ac564cc049885a6e82c5a450 to your computer and use it in GitHub Desktop.
Dangers of mixing blocking and non-blocking code
// 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