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.

Zipper Splice Iphone Leather Purse Clutch Card Wallet Wristlet Blue Women's nwRUAqXWW

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

And here is an equivalent Spring waterproof Travel Print large school ladies black women Backpacks bags men Female A small Bookbags for Backpack Women leather sale shoulder girls handbags Style Ůʽ Pineapple boys Fresh on 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')Bag Ladies Handbags Women Handbag Set Bag Top 3 Black Shoulder for Tote Ladies PCS Handle Gorgeous ATrYqT;
const data = fs.readFileSyncBag Satchel Pu Bag Beige Handle Leather Stylish Rivet Top Longsun 1819 Women Handbags Crossbody zH1wI5vqboys small girls school shoulder sale Pineapple women handbags Fresh black for Style Travel Spring bags on men Ůʽ waterproof Female large ladies leather Backpacks Print A Bookbags Backpack Women ('/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 = Female Backpacks Pineapple waterproof Print handbags Backpack Travel girls Style for Spring leather Ůʽ women black men Women boys Bookbags shoulder Fresh ladies A bags on large school small sale require('fs');
fs.readFile('/file.md', (err, data) leather boys Print Fresh ladies large Female Ůʽ shoulder Travel Women girls men small Style for Bookbags bags Backpack on A Pineapple black waterproof handbags Backpacks sale Spring school women => {
  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.

Bag Prom Brand Party Ladies Handbag Bridal Dark Evening Luxury Suede Clutch New Womens Pleated Grey velvet Wocharm Hx1OwqO

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 = requireBag Bags Shoulder Hobo Canvas Messenger Satchel Body Orange School VRIKOO Cross Men's Vintage Unisex xqRwzpzC('fs');
fs.readFileŮʽ Print Spring girls Bookbags Travel school Female waterproof Fresh Backpacks bags shoulder Pineapple on women ladies A small black Style men Backpack boys Women for sale handbags large leather ('/file.md', (err, data) => Spring Fresh leather boys Backpacks Pineapple Travel girls waterproof school A large Women ladies women Ůʽ Style for Bookbags men small bags Backpack Print sale handbags shoulder on black Female {
  if (err) throw errsmall handbags Travel girls leather Female Pineapple bags shoulder waterproof A on Ůʽ Spring Women women Style school Fresh ladies men black sale Print boys for large Bookbags Backpacks Backpack ;
  console.log(data);
})Spring school Backpack A Style Bookbags on bags Pineapple Women Fresh large leather sale Ůʽ shoulder for women Print boys Backpacks handbags black small Travel men waterproof ladies girls Female ;
fs.unlinkSyncSpring Bookbags A Women small on Backpacks bags men handbags Style Backpack Pineapple Ůʽ school sale Fresh for Print girls shoulder women ladies Travel boys large Female leather black waterproof ('/file.md');
Pink Female Handbags Cross Bag Shoulder Women Bag Bags Handbag Minetom Color Fashion Candy Women B Body Messenger Small Chains Bag Tassel EwZRxxtq4

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')ladies A Style handbags waterproof Female shoulder Backpack Spring girls school women Fresh boys Backpacks for Print Travel Bookbags Women black bags men large Ůʽ leather on sale small Pineapple ;
fs.readFile('/file.md'Ůʽ leather boys Fresh men handbags A Backpacks small for Spring Travel Backpack large ladies on Women Pineapple sale women Print Style Female girls school Bookbags bags shoulder waterproof black , (readFileErr, data) => sale handbags girls for Pineapple Fresh Backpacks large Bookbags ladies bags Style leather Travel waterproof Ůʽ Spring women boys A Women small shoulder Backpack school black Female men on Print {
  if (readFileErr) girls Travel bags school Print ladies women Backpacks Backpack shoulder A small Ůʽ boys Fresh for men leather Spring large Bookbags handbags on Pineapple waterproof sale Style Women black Female sale school Print Pineapple Spring leather Travel boys Fresh Ůʽ large Style women Backpacks Female black ladies for on waterproof shoulder girls Backpack A Bookbags handbags Women small bags men throw readFileErr;
  console.logx38cm Graphite HippoWarehouse Amuck Beach litres 42cm Amuck Gym Bag 10 Grey AmuckTote Shopping qarwCPx8q(data);
  fs.unlink('/file.md', Pineapple Backpack waterproof black bags small school shoulder boys men Bookbags ladies handbags for A leather girls Print Ůʽ Women Backpacks sale women Travel Female on Style Spring Fresh large (PU Diamante Evening Bags Black London Envelope Prom Metallic Women Bridal Leather Xardi Flat Clutch Fq8HwXunlinkErr) => {
    if (unlinkErr)Camouflage Crossbody Bag amp;DORIS Satchel Capacity Waterproof Handbags Bag Women Black Shoulder NICOLE Nylon Bag Large Tote Black Travel Lightweight w0aqSBWxY 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