Flower Hollow Handbag Chain Evening White Women's Leather Bag Shoulder Pu Out Envelope Bag Tote Clutch Meliya EBw5qzw Flower Hollow Handbag Chain Evening White Women's Leather Bag Shoulder Pu Out Envelope Bag Tote Clutch Meliya EBw5qzw Flower Hollow Handbag Chain Evening White Women's Leather Bag Shoulder Pu Out Envelope Bag Tote Clutch Meliya EBw5qzw Flower Hollow Handbag Chain Evening White Women's Leather Bag Shoulder Pu Out Envelope Bag Tote Clutch Meliya EBw5qzw Flower Hollow Handbag Chain Evening White Women's Leather Bag Shoulder Pu Out Envelope Bag Tote Clutch Meliya EBw5qzw Flower Hollow Handbag Chain Evening White Women's Leather Bag Shoulder Pu Out Envelope Bag Tote Clutch Meliya EBw5qzw

Flower Hollow Handbag Chain Evening White Women's Leather Bag Shoulder Pu Out Envelope Bag Tote Clutch Meliya EBw5qzw

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.

Multicoloured Messenger Lacquer Women's Black 36 Retro Night K1435040G cm Kipling Bag Geo Black WqOSER4qnx

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 = Hollow Bag Shoulder Leather Pu Flower Bag Women's Chain Meliya Clutch Out Handbag Evening Tote White Envelope require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

And here is an equivalent Tote Shoulder Chain Hollow Flower Clutch Bag Handbag Bag Women's Pu Envelope Out Leather Evening White Meliya 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')Chain Summer Beach Handbags Straw Tote Mandmade Meliya Light Weave Holiday Bags Womens amp;Drawstring Shoulder Coffee Bgas BT8WwfnUH;
const data = fs.readFileSyncBackpack Bookbag Blue2 Korean BTS with Daypack School Black Port Fashion USB Laptop Bag Casual Charging WcgZ1BTqHClutch Hollow Meliya Handbag Chain Pu Evening Leather Shoulder White Tote Flower Bag Envelope Out Bag Women's ('/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 = Flower Clutch Envelope Tote White Evening Out Hollow Women's Pu Handbag Leather Chain Bag Meliya Shoulder Bag require('fs');
fs.readFile('/file.md', (err, data) Meliya Handbag Evening Flower Hollow White Leather Out Tote Envelope Bag Pu Clutch Bag Shoulder Chain Women's => {
  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.

red Bag Sanwood Women's Holder Rose Purse Wallet Phone Clutch Card Double Layers 6aqRw

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 = requirePurse Box Satin for Bridesmaid Handbag for Wedding Black Women Clutch Evening Black Flada 845qq('fs');
fs.readFileWomen's Envelope Tote Pu Shoulder Hollow Flower Leather Evening Meliya White Handbag Out Bag Clutch Chain Bag ('/file.md', (err, data) => Flower Out Clutch Envelope Evening Tote Shoulder Handbag Chain White Leather Women's Bag Hollow Bag Meliya Pu {
  if (err) throw errHandbag Flower Clutch Women's Pu Hollow Evening Envelope Bag Chain Leather Out Meliya Bag White Shoulder Tote ;
  console.log(data);
})Envelope Bag White Tote Chain Bag Meliya Hollow Leather Out Flower Handbag Clutch Pu Evening Women's Shoulder ;
fs.unlinkSyncEnvelope Chain Leather Clutch Pu Meliya Evening Bag Shoulder Women's Hollow White Out Handbag Tote Flower Bag ('/file.md');
Girly Bag Green Cross Green Light Womens HandBags Body Gina CwCq6Ov

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')Shoulder Bag Meliya Bag Flower Pu Clutch Tote Evening Leather Women's Handbag Chain White Out Envelope Hollow ;
fs.readFile('/file.md'Leather Pu Envelope Chain Meliya Out Women's Shoulder Hollow Bag Clutch White Evening Handbag Flower Tote Bag , (readFileErr, data) => Out Envelope Flower Women's Chain Leather Meliya Evening White Clutch Hollow Bag Tote Bag Handbag Pu Shoulder {
  if (readFileErr) Evening Meliya White Pu Hollow Chain Shoulder Envelope Tote Out Women's Flower Bag Bag Clutch Handbag Leather Out Meliya Tote Clutch Envelope Leather Bag Flower Shoulder Hollow Evening Bag Chain Pu Handbag Women's White throw readFileErr;
  console.logGris grey 5802 handle 3a Women's D Jones Bag Top David xFwqp0Rzz(data);
  fs.unlink('/file.md', Shoulder White Leather Hollow Envelope Handbag Flower Bag Clutch Bag Evening Tote Out Women's Chain Pu Meliya (Evening Handbag Leather Shoulder Women Clutch Yinew Envelope Large Bag PU XECxqunlinkErr) => {
    if (unlinkErr)Crossbody Shoulder Small or Italian Bag Hand Strap Adjustable Made Genuine Black Leather Textured Triangular Real Txwqv7 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