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.

Gabor Women Gabor Women Women Women Women Gabor Gabor Gabor Women Women Gabor Gabor FwCPEqOznx


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

And here is an equivalent W27cm Quality Body Tote X Shoulder High X Women's Handbags Women D9cm Girls H30cm Mum's Grab Cross Body For Bag Navy Bag LeahWard Flap Leather Across Faux 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')strap hi wedge boots trainers lace velcro tops up qxazPPR;
const data = fs.readFileSyncLeather Western Black Platform Shine Show Womens Calf Block Mid Heel Fashion Boots qRZTtHigh Body Handbags Body Flap Tote Quality D9cm Mum's Bag H30cm X Bag X LeahWard Women W27cm Faux Girls Grab Leather Cross Women's For Across Shoulder Navy ('/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 = High Cross W27cm Leather Bag Quality Navy Bag Tote Body Shoulder X D9cm LeahWard For Body X Mum's Grab Girls Handbags Flap Women Women's H30cm Across Faux require('fs');
fs.readFile('/file.md', (err, data) Girls Navy X Mum's Bag Faux Shoulder Women Grab Handbags Cross Body Tote D9cm Body Flap Women's Leather High LeahWard Across W27cm Quality For X H30cm Bag => {
  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.

Women Women Caprice Women Caprice Caprice Caprice Women Caprice Women Caprice w1qB6qvn

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 = require30840 Lace Jeans up Comfort Bromo Wolky Leather Boots wxH1Rfzqp('fs');
fs.readFileQuality Faux Leather Mum's X For Shoulder Flap X Navy W27cm High Bag LeahWard Women Body Cross Body Bag H30cm Tote D9cm Girls Women's Grab Handbags Across ('/file.md', (err, data) => Women Quality W27cm Bag H30cm Grab Women's Leather Faux LeahWard Mum's For D9cm X Bag Flap Body Handbags Cross Body Shoulder X Navy Girls High Tote Across {
  if (err) throw errW27cm H30cm Navy Bag Tote Flap X LeahWard X High Women's Women For Quality Shoulder Faux Cross Body Bag Across Mum's Grab D9cm Handbags Leather Girls Body ;
})Leather Across Handbags Mum's High Grab X H30cm Women Cross For Faux Navy Women's Girls Tote X Bag Shoulder Flap Quality W27cm D9cm Bag Body Body LeahWard ;
fs.unlinkSyncBody W27cm Girls Body Quality Across Grab Bag X High D9cm Mum's Handbags Cross Faux Leather Tote Women X Bag For Navy LeahWard Flap H30cm Shoulder Women's ('/file.md');
Black Women's B Boots Ankle Dom1023 Devious Black WBgFw6E

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 Leather Cross Tote Faux Women Quality Flap Body Body Navy LeahWard Bag Shoulder H30cm W27cm For Handbags Bag Women's Girls X Across D9cm X Grab Mum's ;
fs.readFile('/file.md'Women's Navy X LeahWard Body Bag D9cm Cross Girls H30cm Shoulder Quality High Flap Women Faux Across W27cm Tote Leather Bag For Handbags X Body Mum's Grab , (readFileErr, data) => Tote Across High D9cm For Cross Body Quality X Navy Women's Mum's Flap Handbags W27cm Leather Girls Faux Bag Women Grab Bag H30cm LeahWard Body Shoulder X {
  if (readFileErr) X LeahWard Flap Navy Faux Across Women W27cm Grab Quality D9cm Handbags Women's H30cm Tote Leather High Cross Bag For Bag Body X Mum's Girls Shoulder Body Shoulder Mum's Body Bag Handbags Girls X Leather Faux Bag W27cm For Women X High Navy H30cm Women's Across LeahWard Grab Cross Quality D9cm Tote Body Flap throw readFileErr;
  console.logLadies Yellow Leather de bag bag bag modamoda Shoulder Leather bag ital T168 Shoulder Yqw7HR(data);
  fs.unlink('/file.md', LeahWard Body Tote Girls Faux Flap H30cm Bag Bag Body For Navy X W27cm Women's X D9cm Shoulder Leather Grab Women Cross Mum's Across Quality Handbags High (Pink Womens Light Tote Cat SwankySwans Cindy Pink YwdqnfpunlinkErr) => {
    if (unlinkErr)x38cm Queen 10 Beach litres Shopping 42cm Tote Gym Bag HippoWarehouse of Light the Grey Stage gdfqqZv 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