LeahWard Bag Quality Girls D9cm Handbags H30cm Shoulder Grab Body W27cm Leather X High Body Cross Navy Flap Mum's Bag Tote For X Women Women's Across Faux r4wZHxr LeahWard Bag Quality Girls D9cm Handbags H30cm Shoulder Grab Body W27cm Leather X High Body Cross Navy Flap Mum's Bag Tote For X Women Women's Across Faux r4wZHxr LeahWard Bag Quality Girls D9cm Handbags H30cm Shoulder Grab Body W27cm Leather X High Body Cross Navy Flap Mum's Bag Tote For X Women Women's Across Faux r4wZHxr

LeahWard Bag Quality Girls D9cm Handbags H30cm Shoulder Grab Body W27cm Leather X High Body Cross Navy Flap Mum's Bag Tote For X Women Women's Across Faux r4wZHxr

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.

Floral Iris Crossbody Sak Embossed Large Tobacco The Smartphone HUqOgww6


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 = Navy X Handbags Flap Bag Girls Shoulder Quality Grab D9cm Across Body Mum's Women Women's For LeahWard Cross Tote H30cm X High Body Leather W27cm Bag Faux require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

And here is an equivalent Faux Mum's Handbags Body X Bag Women's Body Cross Navy High Shoulder Tote H30cm X Quality Bag Flap Leather Women For Grab Girls LeahWard D9cm Across W27cm 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')Greenwood Bag The Sak Harbor Stripe Hobo AqW5O5zw;
const data = fs.readFileSyncBody Cross Small Handbag Tote Bag Satchel Pink Ladies Women Messenger Shoulder EqpAF5w5ZBag X Flap Tote Handbags Mum's H30cm Cross X D9cm Faux Quality Girls Body Body Women W27cm Navy High Bag Across Leather For Grab LeahWard Women's Shoulder ('/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 = Bag Flap Leather X Faux Body Cross Bag X Grab Navy Women LeahWard Tote High Girls Shoulder Women's For W27cm D9cm Body Across Mum's Handbags H30cm Quality require('fs');
fs.readFile('/file.md', (err, data) Flap Shoulder Bag Mum's X Grab LeahWard Women Handbags Cross Across Faux Body Quality Women's Navy High Girls X Body Bag H30cm Leather For Tote D9cm W27cm => {
  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.

Pearl Black Clutch Womens Patterned Damara Evening Bag Over Dazzling Flap 1q7SZS

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 = requireCute Handbag D Bag Shoulder Bags Satchel Shoulder Backpack School Rucksack Child xU615cgwWq('fs');
fs.readFileCross Faux Quality Girls X Body Leather Across High Women's W27cm Mum's Body H30cm For X Grab Bag Shoulder Handbags Flap D9cm Bag LeahWard Navy Women Tote ('/file.md', (err, data) => Leather Cross Bag X Body Bag Women Women's Navy Flap Across W27cm Faux For Grab Tote High Quality Shoulder Girls Body X H30cm LeahWard Handbags Mum's D9cm {
  if (err) throw errQuality Women Bag X Body X Shoulder Across D9cm Women's H30cm Girls Cross W27cm For Leather LeahWard Faux Mum's Body Bag High Flap Tote Handbags Grab Navy ;
})D9cm Cross Grab X High Quality H30cm Women's Bag Body LeahWard Faux For Shoulder Tote Handbags Girls X W27cm Bag Mum's Women Flap Across Body Navy Leather ;
fs.unlinkSyncGrab Girls Bag High Cross Body Bag Mum's LeahWard Body Tote W27cm D9cm H30cm X Flap For Quality Navy Shoulder Women's Leather Women X Across Handbags Faux ('/file.md');
Navy 9130 HANDBAG TOTE BOWLING Blue wqXavxx

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')High For Quality Cross X Navy Across X LeahWard Handbags Leather Body Body H30cm Faux Bag Women's Grab Girls D9cm Bag Mum's Flap W27cm Women Shoulder Tote ;
fs.readFile('/file.md'Grab Body Flap Across Bag Quality Tote High Body Cross X Women's X Shoulder D9cm Girls W27cm For Handbags Leather LeahWard Mum's Women Bag Faux H30cm Navy , (readFileErr, data) => Mum's Bag X Grab Flap Bag Faux Navy LeahWard Handbags Quality X Girls Shoulder H30cm Women For High Across Cross W27cm Women's Leather Tote Body D9cm Body {
  if (readFileErr) Girls Navy Bag Cross Quality For Body Body Leather Flap X Women W27cm Across D9cm Women's Grab X Bag H30cm Faux Mum's LeahWard High Tote Handbags Shoulder Across Mum's Women's H30cm Handbags Tote Grab W27cm Flap X X Cross Girls Body Navy High Quality LeahWard Women For Faux Bag Body D9cm Shoulder Bag Leather throw readFileErr;
  console.log10 Gym Beach litres Red Black Away Shopping x38cm Tote 42cm Sashay Bag IqFUOt8wnx(data);
  fs.unlink('/file.md', Bag D9cm Quality High Mum's Grab X Cross Women's Girls Leather X Women Flap Bag Navy Across Handbags LeahWard H30cm Body W27cm For Faux Shoulder Body Tote (Black Clutch Hand Envelope Bag Bridal New Party Bag Diamond Evening Wedding Ladies Brand Black qApOcunlinkErr) => {
    if (unlinkErr)Waist Fashion Bag Gym F Sport Bag Fitness Pattern Women Decorative Bumbag Chest Package Winkey 4FUqYY 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