Skip to content

Instantly share code, notes, and snippets.

@iMichaelOwolabi
Created February 13, 2022 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iMichaelOwolabi/05bedf5465d43e966bc646be3bb78d84 to your computer and use it in GitHub Desktop.
Save iMichaelOwolabi/05bedf5465d43e966bc646be3bb78d84 to your computer and use it in GitHub Desktop.
How to read file in Node.js in a way that it doesn't block the event loop code sample
// A better way of reading file that doesn't block the event loop
const fs = require('fs');
fs.readFile('countries.csv', (error, data) => {
if (error) {
throw new Error(error);
}
console.log(data.toString());
});
// Other JavaScript code below this line will not be blocked which is good.
console.log('OTHER JAVASCRIPT THAT IS NOT BLOCKED');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment