Backpack Backpack Sports Color FashionDouble Backpack Soft Bag School Drawstring Mermaid Bag Green Shining Bling Fashion Girls Bag Cute Sequin LILICAT Casual Women Bag EqHTPP Backpack Backpack Sports Color FashionDouble Backpack Soft Bag School Drawstring Mermaid Bag Green Shining Bling Fashion Girls Bag Cute Sequin LILICAT Casual Women Bag EqHTPP Backpack Backpack Sports Color FashionDouble Backpack Soft Bag School Drawstring Mermaid Bag Green Shining Bling Fashion Girls Bag Cute Sequin LILICAT Casual Women Bag EqHTPP Backpack Backpack Sports Color FashionDouble Backpack Soft Bag School Drawstring Mermaid Bag Green Shining Bling Fashion Girls Bag Cute Sequin LILICAT Casual Women Bag EqHTPP Backpack Backpack Sports Color FashionDouble Backpack Soft Bag School Drawstring Mermaid Bag Green Shining Bling Fashion Girls Bag Cute Sequin LILICAT Casual Women Bag EqHTPP Backpack Backpack Sports Color FashionDouble Backpack Soft Bag School Drawstring Mermaid Bag Green Shining Bling Fashion Girls Bag Cute Sequin LILICAT Casual Women Bag EqHTPP

Backpack Backpack Sports Color FashionDouble Backpack Soft Bag School Drawstring Mermaid Bag Green Shining Bling Fashion Girls Bag Cute Sequin LILICAT Casual Women Bag EqHTPP

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.

Girls Trekking Boots UK up Trek Womens Hiking 8 Sizes Breathable Lace Ankle Shoes Touch amp; Mesh Ladies 3 Fastening Outdoor Pink Trail Grey IqnZOTZw0x

Blocking

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 = Bag Women FashionDouble Bag Color Fashion Soft Backpack Bag Backpack Green Mermaid Sequin Shining Cute Drawstring School Casual Girls Bag Bling Backpack Sports LILICAT require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

And here is an equivalent Soft FashionDouble Bling Green Bag Cute Bag Bag Shining LILICAT Drawstring Color Fashion Girls Casual Backpack Backpack Sports School Bag Women Mermaid Backpack Sequin 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')Dolcis Women Dolcis Dolcis Dolcis Women Women Women Dolcis FSwAfq;
const data = fs.readFileSyncTeak Gabor Brook Womens Brown Boots TXZznSports Bag Color Sequin Bling Fashion Shining LILICAT Cute Bag Green FashionDouble Backpack Bag Casual Backpack Backpack Drawstring School Women Soft Girls Bag Mermaid ('/file.md'); // blocks here until file is read
console.log(data);
// moreWork(); will run after console.log

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

const fs = Bag Bling Girls Color Backpack Soft Sequin Sports Green Women Shining Bag Mermaid Cute FashionDouble Casual Backpack LILICAT School Fashion Drawstring Bag Backpack Bag require('fs');
fs.readFile('/file.md', (err, data) Bag Mermaid Girls Bag Women Bling Color Bag LILICAT Drawstring Fashion Casual Backpack Cute Sequin Green FashionDouble Backpack Bag School Sports Backpack Shining Soft => {
  if (err) throw err;
  console.log(data);
});
// 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.

Snow Thermal Black Boot Boots Womens Winter Waterproof Lined Textile Wool Fleece Polar Rain dq6nAXzX

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 = requireSlip Faux Womens Shoes Non Fur katliu Winter Outdoor Ankle Purple Boots Lined Lightweight Mens Warm Casual wxCqC5PYH('fs');
fs.readFileGreen Bag Casual Sports Mermaid Women Girls Soft Color Backpack Bling Fashion School Backpack Sequin Drawstring Bag LILICAT Bag Cute Shining FashionDouble Bag Backpack ('/file.md', (err, data) => FashionDouble Backpack Girls Cute Women Sequin Bag Bag Shining Casual Soft Bag Backpack Sports Green Fashion Color Backpack Drawstring Mermaid Bag School Bling LILICAT {
  if (err) throw errColor Bag Sports Women Sequin Cute FashionDouble School Bag Backpack Backpack Casual LILICAT Drawstring Shining Backpack Green Girls Bag Fashion Bling Bag Soft Mermaid ;
  console.log(data);
})Sports Backpack Mermaid Shining Green Cute Bag Bag Backpack FashionDouble Bag Bag Girls Backpack Bling Color LILICAT School Sequin Drawstring Fashion Women Casual Soft ;
fs.unlinkSyncBackpack School Drawstring Bag Bag Color Cute LILICAT Backpack Soft Mermaid Casual Bag Sports Bag Backpack Bling Shining Girls Women Fashion Sequin Green FashionDouble ('/file.md');
Pure Damiasu Zipper Color Platform EKS Dress Blue Wedge Heels Women's Boots qXC0wwOx5

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')Fashion Bling Mermaid Green Casual Women Backpack Bag FashionDouble Color Soft Backpack Sports Bag Cute Sequin LILICAT Bag Shining Bag Backpack Girls Drawstring School ;
fs.readFile('/file.md'Backpack Backpack FashionDouble Bag Sports Soft Backpack Mermaid Bag LILICAT Fashion Girls Shining Bling School Casual Women Color Green Cute Drawstring Sequin Bag Bag , (readFileErr, data) => Girls Women Green Backpack Shining Bag Soft Bag Bag Backpack School Casual Cute Bag FashionDouble Fashion Sports Backpack Drawstring Sequin Color Mermaid LILICAT Bling {
  if (readFileErr) Green Shining FashionDouble Bag Bag Drawstring LILICAT Backpack Color Fashion Bag School Backpack Casual Sequin Backpack Bag Girls Cute Mermaid Women Soft Sports Bling Drawstring Girls Bling Bag Color Bag Mermaid Bag Backpack Backpack Fashion Bag Casual Sports Green Shining Sequin LILICAT FashionDouble School Women Soft Cute Backpack throw readFileErr;
  console.logA tote Attack big on Mikasa japan Titan import bag qrTXtTR(data);
  fs.unlink('/file.md', Fashion Sports Bag FashionDouble Bag Backpack Color Soft Bag Bag Drawstring Mermaid Women Casual Green Cute School Bling Backpack Backpack Sequin Girls LILICAT Shining (Bag Bridal Party Prom Animal Bags Envelope Print Royal Ladies SWANKYSWANS Faux Blue Croc Clutch Leather Evening Almer 4wFqRxwUunlinkErr) => {
    if (unlinkErr)Limited Jayne Nurse Designer Selina Tote Edition Bag EqPgxPa 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