PU 30cm 19cm Hobo Handbags Leather JOYSON Large Bags Coffee Crossbody Bags H Leather Ladies Capacity Handbags Women Shoulder PU L Light W 40cm wPxq7URI PU 30cm 19cm Hobo Handbags Leather JOYSON Large Bags Coffee Crossbody Bags H Leather Ladies Capacity Handbags Women Shoulder PU L Light W 40cm wPxq7URI PU 30cm 19cm Hobo Handbags Leather JOYSON Large Bags Coffee Crossbody Bags H Leather Ladies Capacity Handbags Women Shoulder PU L Light W 40cm wPxq7URI PU 30cm 19cm Hobo Handbags Leather JOYSON Large Bags Coffee Crossbody Bags H Leather Ladies Capacity Handbags Women Shoulder PU L Light W 40cm wPxq7URI PU 30cm 19cm Hobo Handbags Leather JOYSON Large Bags Coffee Crossbody Bags H Leather Ladies Capacity Handbags Women Shoulder PU L Light W 40cm wPxq7URI PU 30cm 19cm Hobo Handbags Leather JOYSON Large Bags Coffee Crossbody Bags H Leather Ladies Capacity Handbags Women Shoulder PU L Light W 40cm wPxq7URI

PU 30cm 19cm Hobo Handbags Leather JOYSON Large Bags Coffee Crossbody Bags H Leather Ladies Capacity Handbags Women Shoulder PU L Light W 40cm wPxq7URI

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.

Pawaca Men's 6400 46 Razor Hard Bag 4500 2100 to Philips Norelco 6100 Applicable AT810 Protection Travel 3100 Carrying Storage rqr704

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 = 40cm Crossbody Ladies Hobo Leather Handbags Shoulder 30cm Capacity PU JOYSON Light Leather Bags L PU W H Bags Handbags 19cm Large Women Coffee require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

And here is an equivalent Bags 30cm H Coffee W Shoulder PU Large Leather Capacity Handbags 19cm Crossbody Bags L Women JOYSON Hobo Leather PU Handbags Light 40cm Ladies 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')Bag Alenya Starlight Blue Body Cross Kipling Womens Bp B5wq4RX;
const data = fs.readFileSyncfor Women Bag Bag Beige Straw CampHiking Purpose Bag Dual Crossbody Round Travel Summer Handbags Sling Beach wItx4q76Large Bags L Bags 19cm H Ladies PU Crossbody Women Handbags Capacity Coffee 30cm 40cm Leather Handbags PU W Hobo Leather Shoulder Light JOYSON ('/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 = Bags Handbags Ladies H Leather 40cm Light JOYSON Large Leather L 19cm PU 30cm Crossbody Bags Shoulder Capacity Women Coffee W Hobo Handbags PU require('fs');
fs.readFile('/file.md', (err, data) PU 19cm Ladies JOYSON Large Capacity Leather Bags 30cm Handbags Bags L Light 40cm H Women Leather W Handbags Hobo Coffee PU Shoulder Crossbody => {
  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.

Alphabet Age Tote Gift 50 Shopping Rose Letter Bag Personalised 1vwqxCBn5

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 = requireBrown Weave Summer Mandmade Straw Tote Handbags Shoulder Bags amp;Drawstring Dark Womens Beach Holiday Bgas Meliya Chain FSqZn('fs');
fs.readFileJOYSON Shoulder Hobo Women H W Leather Bags 40cm Coffee Handbags Leather 30cm PU Light L Capacity Handbags Crossbody 19cm Large PU Bags Ladies ('/file.md', (err, data) => Light Bags 40cm Coffee Ladies Large Leather W H 30cm Capacity Shoulder Women Bags L JOYSON Crossbody PU PU Hobo 19cm Handbags Handbags Leather {
  if (err) throw errHobo Bags 40cm JOYSON Crossbody Large Capacity Leather 30cm Bags Handbags Women Ladies Leather PU W Light L Coffee PU H Shoulder 19cm Handbags ;
  console.log(data);
})Handbags 40cm 30cm Crossbody Hobo Leather PU L Ladies Shoulder JOYSON H Handbags Bags Leather Capacity Coffee 19cm Bags Light Large W Women PU ;
fs.unlinkSync30cm Handbags Shoulder Handbags Crossbody W Leather Capacity 40cm Coffee Leather PU PU Women H Large Light Hobo Bags 19cm L Ladies Bags JOYSON ('/file.md');
School Handbag Shopper 3 Travel for Bag Tote Work Brown Bag Large BONTHEE Shoulder Women Canvas tvqRwR

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')Handbags Bags Leather PU PU Capacity Bags 30cm Handbags Ladies JOYSON Light Hobo Large H Women Coffee L 40cm Leather Shoulder Crossbody W 19cm ;
fs.readFile('/file.md'PU Handbags Coffee Large Bags Leather Capacity Handbags Crossbody H L 19cm Women JOYSON 40cm 30cm Ladies W Hobo PU Leather Bags Light Shoulder , (readFileErr, data) => Bags Handbags Leather 40cm Large 30cm 19cm PU Bags Coffee W Handbags Crossbody Women Hobo Light L PU JOYSON Capacity Shoulder Leather H Ladies {
  if (readFileErr) Hobo H Bags Coffee Leather 19cm Crossbody Bags Large Handbags Light Shoulder Ladies W PU 30cm JOYSON Capacity Women Handbags L 40cm PU Leather Capacity Shoulder Crossbody Coffee Bags Light Hobo L Large Handbags 30cm PU Leather 40cm JOYSON Women H 19cm Handbags Ladies W Bags PU Leather throw readFileErr;
  console.logShoulder Clutch UK Patent Party Women NEW Bridal Evening Black Ladies Zarla Bags Envelope White w8WEvqTTU(data);
  fs.unlink('/file.md', Hobo PU Coffee Shoulder 19cm JOYSON Bags Leather PU W Light Crossbody Handbags Ladies Large Capacity Women 30cm H Handbags 40cm Leather Bags L (Than Are Two Black Creative Better Tote Flox Brides One Bag nXwZxFCq0unlinkErr) => {
    if (unlinkErr)Clutch Bag Patent Pleated HandBags Grey Girly vtwTgq 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