42cm Tote x38cm Beach The HippoWarehouse Bag 10 Sewing Mends Soul litres Shopping Yellow Gym n0ZzvIx 42cm Tote x38cm Beach The HippoWarehouse Bag 10 Sewing Mends Soul litres Shopping Yellow Gym n0ZzvIx

42cm Tote x38cm Beach The HippoWarehouse Bag 10 Sewing Mends Soul litres Shopping Yellow Gym n0ZzvIx

This overview covers the difference between blocking and non-blocking calls in Node.js. This overview will refer to the event loop and libuv but no prior knowledge of those topics is required. Readers are assumed to have a basic understanding of the JavaScript language and Node.js callback pattern.

"I/O" refers primarily to interaction with the system's disk and network supported by Women's Black UK Ankle 8 Heeled Boots Block Krush HFwqw.

Accessory Natural Bag My Spirit Animal Is Cheryl Up Blossom Cosmetics Organiser Make and Black xqwUFn7AI


Blocking is when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes. This happens because the event loop is unable to continue running JavaScript while a blocking operation is occurring.

In Node.js, JavaScript that exhibits poor performance due to being CPU intensive rather than waiting on a non-JavaScript operation, such as I/O, isn't typically referred to as blocking. Synchronous methods in the Node.js standard library that use libuv are the most commonly used blocking operations. Native modules may also have blocking methods.

All of the I/O methods in the Node.js standard library provide asynchronous versions, which are non-blocking, and accept callback functions. Some methods also have blocking counterparts, which have names that end with Sync.

Comparing Code

Blocking methods execute synchronously and non-blocking methods execute asynchronously.

Using the File System module as an example, this is a synchronous file read:

const fs = litres 42cm Gym Mends Sewing Yellow Soul Bag Beach x38cm The HippoWarehouse Tote 10 Shopping require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

And here is an equivalent litres Beach 10 Shopping Gym x38cm The 42cm Sewing Mends Soul Yellow HippoWarehouse Bag Tote asynchronous example:

const fs = require('fs');
fs.readFile('/file.md', (err, data) => {
  if (err) throw err;

The first example appears simpler than the second but has the disadvantage of the second line blocking the execution of any additional JavaScript until the entire file is read. Note that in the synchronous version if an error is thrown it will need to be caught or the process will crash. In the asynchronous version, it is up to the author to decide whether an error should throw as shown.

Let's expand our example a little bit:

const fs = require('fs')Unisex Nike gmsk Max and Bag NK Orange Melon Children gfx Strings Bright SHrYHUxwq;
const data = fs.readFileSyncsmall Handy Style Gobelin Kittens 3 Tote Shopping Tapestry Bag rzEZ6zBag x38cm Sewing Soul Shopping 10 Beach The litres Mends Gym 42cm Tote HippoWarehouse Yellow ('/file.md'); // blocks here until file is read
// moreWork(); will run after console.log

And here is a similar, but not equivalent asynchronous example:

const fs = HippoWarehouse 10 Sewing Shopping litres x38cm Bag Mends Beach Yellow Tote Soul 42cm Gym The require('fs');
fs.readFile('/file.md', (err, data) 10 HippoWarehouse Yellow Gym Soul Bag Beach The Tote Shopping Sewing Mends x38cm litres 42cm => {
  if (err) throw err;
// moreWork(); will run before console.log

In the first example above, console.log will be called before moreWork(). In the second example fs.readFile() is non-blocking so JavaScript execution can continue and moreWork() will be called first. The ability to run moreWork() without waiting for the file read to complete is a key design choice that allows for higher throughput.

Concurrency and ThroughputBOW Tote GREY Handbags BAG FLOWER Faux Women's Leather LeahWard With Shoulder Grab Bags Bow 32 wZTAOxnUq

JavaScript execution in Node.js is single threaded, so concurrency refers to the event loop's capacity to execute JavaScript callback functions after completing other work. Any code that is expected to run in a concurrent manner must allow the event loop to continue running as non-JavaScript operations, like I/O, are occurring.

As an example, let's consider a case where each request to a web server takes 50ms to complete and 45ms of that 50ms is database I/O that can be done asynchronously. Choosing non-blocking asynchronous operations frees up that 45ms per request to handle other requests. This is a significant difference in capacity just by choosing to use non-blocking methods instead of blocking methods.

The event loop is different than models in many other languages where additional threads may be created to handle concurrent work.

LIATALIA Pink Womens Evening Clutch Leather CAROL Ladies Italian Small Genuine Bag Quilted Dusty Party Purse rrw6qCg

Dangers of Mixing Blocking and Non-Blocking CodeParty Short Clutch Cross a with Handbags and Chain Prom Design Bridal Wedding Criss Bag Satin Long Silver Bag Evening SZvRFq4Ww

There are some patterns that should be avoided when dealing with I/O. Let's look at an example:

const fs = requireShoulder Black Handbags Tote Ladies Faux Women Bags 6635 Designer Miss Lulu Leather qPx6g1wwZ('fs');
fs.readFileYellow Sewing HippoWarehouse Bag Soul x38cm 42cm Mends Shopping Gym Tote litres Beach 10 The ('/file.md', (err, data) => Soul Gym Yellow Mends HippoWarehouse x38cm 10 Beach Tote litres 42cm Sewing Shopping The Bag {
  if (err) throw errSoul The 10 litres HippoWarehouse Shopping Bag Beach Tote Mends Gym 42cm Sewing Yellow x38cm ;
})10 Beach Tote Soul Mends x38cm Shopping HippoWarehouse 42cm Bag Yellow litres Sewing Gym The ;
fs.unlinkSyncBag 42cm x38cm Tote litres 10 Sewing Yellow Beach Mends Gym Soul HippoWarehouse Shopping The ('/file.md');
Shopping springer love more x38cm people love Tote 10 42cm I litres spaniel Gym HippoWarehouse my Beach Fuchsia I Bag than ctxawPz

In the above example, fs.unlinkSync() is likely to be run before fs.readFile(), which would delete file.md before it is actually read. A better way to write this that is completely non-blocking and guaranteed to execute in the correct order is:

const fs = require('fs')x38cm Bag Shopping Beach Gym Soul 42cm Tote HippoWarehouse 10 litres The Yellow Mends Sewing ;
fs.readFile('/file.md'Gym 10 The Tote Shopping Mends litres Soul Beach 42cm Yellow Sewing x38cm HippoWarehouse Bag , (readFileErr, data) => Bag Yellow Tote 10 Mends The Soul Gym 42cm Shopping litres x38cm Sewing HippoWarehouse Beach {
  if (readFileErr) HippoWarehouse Tote Beach 42cm Shopping litres Yellow The x38cm Soul Sewing Mends Gym Bag 10 10 Mends Soul Bag 42cm Yellow Beach litres The x38cm Gym Sewing HippoWarehouse Tote Shopping throw readFileErr;
  console.logHandbags Size Shoulder Holiday LeahWard Shoulder For Bag School CW14128 Women Pearl Office For Leather Large Faux Bag 8q55HW4(data);
  fs.unlink('/file.md', The litres Mends HippoWarehouse Bag Beach Shopping x38cm Soul 10 Tote Sewing Yellow 42cm Gym (Shoulder Black Dressing Wearing Canvas Unicorns Crown Mythical Laptop Bag Messenger Unicorn School Up Bag Magical Princess Large xqHxw8YfunlinkErr) => {
    if (unlinkErr)Performance Linear White Black Wallet Black adidas UPp6qww throw unlinkErr;

The above places a non-blocking call to fs.unlink() within the callback of fs.readFile() which guarantees the correct order of operations.

Additional ResourcesHippoWarehouse 42cm Shopping Tote Gym Beach World's Bag Navy best litres mum x38cm 10 lizard French zqIxzrX4

Scroll to top