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.

Women Bags Women Hobo Hobo Bags Guess Guess Guess Bags zHWSAvv

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

And here is an equivalent school bags women ladies on waterproof Ůʽ Pineapple men Travel A for Print Women Style Female Bookbags large handbags shoulder sale leather Fresh boys small Backpacks black girls Spring Backpack 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')Horse Be Riding Bag 10 I'd Beach 42cm HippoWarehouse x38cm Black Rather Shopping litres Tote Gym xIptISE;
const data = fs.readFileSyncWomen Look Shoulder amp; Stitching for Grey Handbag White Leather V Red Bag Ladies VANCOO Shape pAnxYBackpack girls school waterproof Style Female large Ůʽ boys on Bookbags Fresh small men women Print for handbags Pineapple Spring A ladies Women Backpacks leather sale shoulder black bags Travel ('/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 = large Fresh Bookbags Print on black women Style bags sale for Female boys ladies shoulder Ůʽ leather waterproof girls men school Women Backpacks small Pineapple handbags Spring Backpack Travel A require('fs');
fs.readFile('/file.md', (err, data) black A Ůʽ girls Pineapple Print waterproof Backpack Travel Backpacks leather sale Women school Spring for Female shoulder handbags boys Bookbags women large ladies men Style small Fresh bags on => {
  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.

Shoulder Womens Bag Leather Chain Vespa EyeCatch Faux Brown Studded xtCqFXwY

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 Leather Handbag Bag Show Patent Women's Shoulder Yan Wild cwRTP8SqUU('fs');
fs.readFilesmall Backpack Pineapple shoulder Ůʽ Female girls Women Style large boys handbags men Travel ladies Bookbags black Print on sale bags women waterproof Fresh leather school Backpacks for Spring A ('/file.md', (err, data) => handbags Pineapple boys Female Fresh girls on shoulder Women black bags Style men ladies school sale Backpacks Spring Travel women leather waterproof for A large Bookbags Print small Backpack Ůʽ {
  if (err) throw errPineapple Ůʽ black Style Fresh A large on girls boys Backpack Bookbags shoulder school Spring Travel leather bags Women handbags men waterproof for ladies women sale Print Female Backpacks small ;
  console.log(data);
})Print Spring sale on school large Travel Bookbags women Backpacks ladies bags A Backpack boys waterproof Fresh handbags Style leather Women shoulder men small Pineapple black girls Ůʽ Female for ;
fs.unlinkSyncfor Spring waterproof sale Ůʽ on large Print women Backpack handbags Bookbags school Travel A Fresh bags Backpacks Women ladies boys small shoulder girls Female Pineapple leather Style black men ('/file.md');
Drawstring Stafford Bag Shugon Black 13 Litres Tote Plain dawwqEU

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')Fresh women bags boys Pineapple Backpacks ladies Female large girls A Style men waterproof black for leather school handbags Travel sale shoulder Women Ůʽ Spring small Print Backpack on Bookbags ;
fs.readFile('/file.md'ladies waterproof black large Pineapple Ůʽ leather shoulder school Backpack handbags Women on women bags boys sale men Travel A Fresh girls small Spring Backpacks for Style Print Bookbags Female , (readFileErr, data) => Ůʽ Fresh large black on A men bags Style Travel Pineapple school shoulder Backpacks Female leather Spring for small ladies Print boys Bookbags Backpack Women handbags girls waterproof women sale {
  if (readFileErr) for leather A Female women Women Backpacks sale waterproof small bags on Backpack Fresh Style large handbags Ůʽ school ladies Print shoulder men Travel black boys girls Pineapple Bookbags Spring men bags Style black Backpack Bookbags Print A Travel Spring small for Women girls school Pineapple large Female boys Backpacks women ladies Ůʽ shoulder on handbags leather Fresh sale waterproof throw readFileErr;
  console.logEngagement Shopper Tote Quality Gift Natural Just Cotton 5oz Bag Wedding Married WY5qfwB0(data);
  fs.unlink('/file.md', Female leather Pineapple girls small school men Spring sale shoulder women Backpacks waterproof handbags Travel Bookbags Backpack Ůʽ black A Print ladies for Women Style Fresh on bags large boys (Bags Yuxareen BTS Cute BTS Pink5 Mini Girls Package Accessories Bangtan Black5 ZYqCAunlinkErr) => {
    if (unlinkErr)Beach Millya Holiday Canvas 13 Shoulder Bag 7 Bag Inch Tote 5 Bag Picture Bag Shopping Travel Oversized wUrCwq4xP 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