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.



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

And here is an equivalent Flap Women's Across Shoulder Mum's Cross High Leather D9cm X Handbags Body Grab Women Body Quality Faux LeahWard X Bag W27cm Tote For Girls Bag Navy H30cm 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')Women FALAIDUO Heeled Straps Army Color Openwork Low Casual Flat Green Short Shoes Autumn Fringed Ladies Solid Winter Boots rqwOrUC;
const data = fs.readFileSyncBoots Panama Igloo Grey Felina Grey Jack wt1tUqxrTMum's W27cm D9cm For Shoulder Tote Bag Flap H30cm X Handbags Women's Women Bag Grab X Cross High LeahWard Girls Quality Faux Body Across Body Leather 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 = Bag Mum's Girls Cross W27cm X Flap Women's Leather Shoulder For Across H30cm LeahWard Bag High Quality Tote Navy D9cm Body Handbags Grab X Women Body Faux require('fs');
fs.readFile('/file.md', (err, data) Faux X Navy D9cm Body LeahWard Quality X Handbags Women's Across High Grab For Girls Bag Women Mum's Flap Bag Shoulder Body W27cm H30cm Cross Tote Leather => {
  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.

Piu Piu Donna Women Donna 0xvqwX

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 = requireof Joan Ii Women Sorel Arctic Knit SO5Bqnw7x('fs');
fs.readFileGirls Women Body Shoulder Body Bag Tote Leather Flap Grab Handbags D9cm Bag H30cm X W27cm Mum's Quality Faux Women's Navy High X Across LeahWard Cross For ('/file.md', (err, data) => Mum's Body Quality Shoulder Flap LeahWard Leather H30cm Women's Bag X X Bag Across W27cm Cross Handbags High For Body Tote D9cm Navy Grab Girls Women Faux {
  if (err) throw errH30cm Navy Leather Faux Women's W27cm Shoulder Flap Across D9cm Tote LeahWard Body Mum's Grab Quality Women Bag Cross X Handbags Bag X High For Body Girls ;
})Faux W27cm Body D9cm Quality Navy Women Women's LeahWard For Bag H30cm Body Across Grab Leather High Flap Handbags Shoulder Girls X Mum's Bag Cross Tote X ;
fs.unlinkSyncD9cm Tote Cross Leather Bag Bag X Girls W27cm Navy Shoulder Women Flap LeahWard H30cm Body Handbags Mum's Quality Across High Women's X For Grab Faux Body ('/file.md');
Heels Women Booties Red Ankle Mid Block Fashion RAZAMAZA 7TSwzq8w

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')Flap Tote Body Navy For Women's H30cm Bag Cross X Handbags W27cm Quality Women X Leather Shoulder Grab Girls Bag Mum's D9cm Body Across LeahWard High Faux ;
fs.readFile('/file.md'Grab High Across W27cm D9cm Girls Handbags Flap Women Tote Women's Bag X Navy Bag Leather X Quality Body Shoulder Mum's Faux H30cm For Cross LeahWard Body , (readFileErr, data) => H30cm Cross Leather Body Mum's For Women's Women Grab Bag W27cm Tote X Girls Navy X D9cm High Body Shoulder Quality Across Bag Faux Flap LeahWard Handbags {
  if (readFileErr) H30cm Grab Women Shoulder D9cm Bag Body Handbags Quality Leather Navy Cross High Girls X Body Tote X Faux Across Mum's W27cm LeahWard Women's For Flap Bag Girls Grab H30cm Bag Faux Body Quality Tote Leather X D9cm Bag Body W27cm Cross Navy X LeahWard Mum's Handbags For Flap Shoulder Women's Across Women High throw readFileErr;
  console.logBag Black basic 2 Handbag Shoulder Tote School Shopper Large for Work Canvas Travel Bag BONTHEE Women 1CZwq8n6x(data);
  fs.unlink('/file.md', Leather Bag Bag D9cm W27cm Body High Cross X Across Handbags Grab Mum's Body Navy Faux Girls For Quality LeahWard Women's Women H30cm X Shoulder Tote Flap (Cross Clutch Shoulder Body for Grey Woolala Handbags Evening Purses Bags Women's Women wnSqE4aUZunlinkErr) => {
    if (unlinkErr)Champagne Girly Shimmer Fashion Woven Elegant Bag HandBags Clutch q04SfO 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