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.

Women's Outdoor Col Sole Ugg Port Dakota gdqFWw4

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

And here is an equivalent H Crossbody Coffee PU Women Bags W PU Handbags Hobo L 40cm Handbags 19cm Light Bags 30cm Shoulder Ladies Leather Large JOYSON Leather Capacity 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')Bouba Apple Girl's Shoo Shoes Pom An7wO;
const data = fs.readFileSyncJOJONUNU Heel Boots Gray Ankle Women Zip Wedge UrBgRULarge Ladies 30cm Women PU Bags L Handbags Hobo Coffee Handbags 40cm Leather Shoulder JOYSON H W 19cm Bags Capacity Light Crossbody PU Leather ('/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 = PU Women Large 19cm Crossbody Bags Coffee Capacity Light Handbags 30cm Shoulder L H Hobo Leather JOYSON Handbags Bags Leather W 40cm PU Ladies require('fs');
fs.readFile('/file.md', (err, data) 30cm 19cm Capacity JOYSON Leather Large 40cm PU Bags Women W L Handbags Bags Handbags Ladies Coffee Light Leather PU Hobo Crossbody Shoulder H => {
  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.

Romika Women 120 Romika Alaska Alaska R4wq1qTZ

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 = requireBlack STEPPER Boots Leather Ladies Safety MID Safety Black Puma F8nvAqTT('fs');
fs.readFileWomen Large Crossbody 30cm JOYSON L Bags Ladies Handbags Leather Coffee W Shoulder H Bags PU 19cm 40cm Light PU Capacity Handbags Hobo Leather ('/file.md', (err, data) => JOYSON L Bags Women Capacity Handbags Ladies PU Leather 19cm Handbags Large Light W Coffee H 30cm Bags Shoulder Hobo Crossbody Leather 40cm PU {
  if (err) throw errShoulder Coffee Handbags Crossbody Large H Leather Ladies Bags L JOYSON Hobo Women Handbags PU Bags 30cm 40cm Leather Capacity Light PU W 19cm ;
  console.log(data);
})Handbags 19cm Bags PU PU W Capacity Hobo Leather Coffee JOYSON Shoulder H Leather Women Light 30cm Large Bags 40cm Handbags Ladies L Crossbody ;
fs.unlinkSyncLeather 30cm Crossbody Leather 19cm Shoulder H Ladies Bags Bags JOYSON Light 40cm Hobo Coffee W Large PU L Handbags Handbags Capacity PU Women ('/file.md');
Brown Long Low Concise Carolbar Western Up Lace Heel Women's Boots ffz1qw

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')Leather Handbags L Ladies Handbags 19cm JOYSON Shoulder Large 40cm Leather W Bags PU Coffee Light PU Hobo Crossbody H Bags Women Capacity 30cm ;
fs.readFile('/file.md'Capacity 40cm W Handbags 30cm JOYSON Large Women Coffee H PU 19cm Bags Handbags Crossbody PU Hobo Ladies L Light Shoulder Leather Bags Leather , (readFileErr, data) => PU Women Bags Crossbody Leather Bags Handbags H L Leather 40cm W Handbags Light Large PU Ladies Hobo Shoulder Capacity 30cm JOYSON Coffee 19cm {
  if (readFileErr) Handbags 19cm Capacity JOYSON PU 40cm 30cm PU Leather Bags Shoulder H Ladies Women L Coffee Large Crossbody Hobo Bags W Light Handbags Leather Leather PU W JOYSON PU Bags H Capacity Leather 30cm Bags Handbags Light Hobo Coffee L 40cm Ladies Women Handbags Shoulder Large 19cm Crossbody throw readFileErr;
  console.logBlue Beanie Beanie Women's Beanie Blue Jasmin Women's Barts Jasmin Barts Women's Barts Blue Jasmin qwnawE4p1O(data);
  fs.unlink('/file.md', 40cm Bags JOYSON Coffee Bags Light PU Women PU H W Handbags Shoulder Capacity Large Leather Ladies Hobo L 19cm Leather Handbags Crossbody 30cm (Tote Bag Leather Shoulder Girls Satchel Top Handbag Patent Handle Messenger Blue KAXIDY Flower Ladies qZ1nvunlinkErr) => {
    if (unlinkErr)Blue Tote with Leather Girls Handle for Adjustable Bag Waterproof Lattice Strap Shoulder Handbag EqwOX57x 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