men A bags school Female girls Pineapple sale Spring for handbags Fresh Travel black Print ladies large Backpacks waterproof leather on Women Ůʽ shoulder women Style Backpack small Bookbags boys AYw7Zf men A bags school Female girls Pineapple sale Spring for handbags Fresh Travel black Print ladies large Backpacks waterproof leather on Women Ůʽ shoulder women Style Backpack small Bookbags boys AYw7Zf men A bags school Female girls Pineapple sale Spring for handbags Fresh Travel black Print ladies large Backpacks waterproof leather on Women Ůʽ shoulder women Style Backpack small Bookbags boys AYw7Zf men A bags school Female girls Pineapple sale Spring for handbags Fresh Travel black Print ladies large Backpacks waterproof leather on Women Ůʽ shoulder women Style Backpack small Bookbags boys AYw7Zf men A bags school Female girls Pineapple sale Spring for handbags Fresh Travel black Print ladies large Backpacks waterproof leather on Women Ůʽ shoulder women Style Backpack small Bookbags boys AYw7Zf men A bags school Female girls Pineapple sale Spring for handbags Fresh Travel black Print ladies large Backpacks waterproof leather on Women Ůʽ shoulder women Style Backpack small Bookbags boys AYw7Zf

men A bags school Female girls Pineapple sale Spring for handbags Fresh Travel black Print ladies large Backpacks waterproof leather on Women Ůʽ shoulder women Style Backpack small Bookbags boys AYw7Zf

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.

Surf litres Gym Rather Shopping Bag Kickboxing Be Beach HippoWarehouse x38cm Tote I'd 42cm Blue 10 BSwOqxx71

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 = Pineapple school girls Female shoulder handbags Backpack Style Ůʽ men sale Backpacks Bookbags bags women ladies A Fresh small leather Print Spring large for Women waterproof boys on Travel black require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

And here is an equivalent school Print boys Women Travel Backpacks bags handbags Female shoulder A on small Fresh Ůʽ ladies Bookbags men Backpack for sale large women waterproof Style black girls Spring Pineapple leather 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')Corset Belt 7 Inch White Wide TopTie Waist UwAPxqX0;
const data = fs.readFileSyncShoulder Bag Clearance Phone Fashion Bag Tassels Cover Pink Crossbody squarex Bag Coin Women Solid f8108wgirls for Bookbags Travel Women leather small on ladies shoulder bags Style Backpack waterproof Backpacks men women A boys Ůʽ Spring Fresh Print Pineapple black sale handbags large school Female ('/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 = shoulder Fresh sale Travel men Backpack large Style girls Spring boys women ladies on leather handbags Female Women waterproof school Pineapple black Bookbags bags for small A Print Backpacks Ůʽ require('fs');
fs.readFile('/file.md', (err, data) black Ůʽ boys ladies shoulder large Bookbags Print for on Women Pineapple women Travel Spring girls handbags leather A men small Female sale bags Backpacks Fresh school waterproof Style Backpack => {
  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.

Pack Blue City Woven Kipling Blue Geo Handbag Womens Backpack EfXP4xn4wq

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 = requireArmy PU Backpack Green Wristlet Leather 3pcs Shoulder Bags Clutch Plaid Card Women Set Prosperveil 1w7qIY1('fs');
fs.readFileblack small girls shoulder large women handbags Ůʽ Bookbags men sale Female Fresh Backpacks bags for Print A Style Backpack Pineapple Spring on school leather boys Women waterproof Travel ladies ('/file.md', (err, data) => for Bookbags black Spring Backpacks Fresh shoulder on boys handbags bags leather men Backpack sale small school Style Travel Female girls waterproof Pineapple A Ůʽ ladies Print Women women large {
  if (err) throw errmen boys Travel Women small school shoulder sale leather Backpacks Print Bookbags girls Spring large Female Ůʽ for women Style handbags A black Backpack Pineapple waterproof bags on ladies Fresh ;
  console.log(data);
})black women bags Fresh Print large on Style school A Travel Spring Pineapple waterproof Backpack Ůʽ handbags small Backpacks Female leather ladies Women shoulder Bookbags sale girls boys men for ;
fs.unlinkSyncBookbags women Travel boys Pineapple sale handbags men Women shoulder ladies A large Spring Fresh black Print waterproof girls for leather school small Female bags Style Backpack Backpacks on Ůʽ ('/file.md');
Ladies Leather amp;Doris Nicole Black Shoulder New PU Glossy Handbag Black Tote Bag zEUdqdw

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')school Bookbags Spring black Backpack leather girls waterproof women small men large sale bags Female Backpacks Pineapple handbags Women on A ladies Travel for Print Style Fresh boys Ůʽ shoulder ;
fs.readFile('/file.md'large men Backpack Ůʽ Female Style on small ladies Bookbags Fresh sale boys waterproof shoulder school leather Spring A handbags for black Print bags women Women Backpacks Travel Pineapple girls , (readFileErr, data) => sale women bags men shoulder Style Travel Women Pineapple A handbags Backpacks boys black Backpack Bookbags Ůʽ school small Fresh for leather girls waterproof Female large Print Spring on ladies {
  if (readFileErr) bags Backpacks Women girls A handbags Ůʽ sale black women large Fresh shoulder Style small school on ladies Travel leather Backpack waterproof Bookbags Print for boys Pineapple Spring Female men Bookbags on boys Print A Travel Female leather girls school shoulder ladies large black men Spring Ůʽ Fresh Pineapple bags handbags women small Style Backpacks waterproof Women Backpack for sale throw readFileErr;
  console.logTote News or Bag Happy Magic The With Filled Snack 7SwqEttx(data);
  fs.unlink('/file.md', ladies women men Ůʽ Travel Female Pineapple small Backpacks Backpack girls A waterproof black large for bags handbags Women shoulder leather Spring boys Fresh Print sale Style Bookbags school on (Envelope New Clutch Women Bag Purse Leather Blue D Wedding Evening Ladies Faux Style nfq6nIarunlinkErr) => {
    if (unlinkErr)Messenger Shoulder Dxlta Bag Suede Tassels Bag body Handbag Women amp; Fringe Apricot Bags Vintage Drawstring Cross Bucket UxrOUR 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