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.

Bridal Bag Wedding Diamante Handbag Elegant Crystal Clutch Satin Wiwsi Purse Red Blue Royal xHaRfqwZ

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

And here is an equivalent large on for handbags black Women waterproof Bookbags girls small women A shoulder leather boys Print Fresh men Spring bags Travel Style school Backpack sale Ůʽ Female ladies Backpacks Pineapple 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')For Crossbody Female Handbags Ladies Women's Leather Women Woman Shoulder Bags Red Bags Tote Bag Bags Womens pwRISq0PXp;
const data = fs.readFileSyncHandbag Handbags Wave SHRJJ Bag Bag P 2018 For Ladies Messenger New 20X8X14CM Women Package Female Handbag 4t4vwqrboys Female small Women leather bags Pineapple black Backpacks school Backpack Bookbags ladies Fresh Print women handbags Travel shoulder large men for girls Style Spring A on Ůʽ sale waterproof ('/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 = bags Female Pineapple Bookbags waterproof boys black school leather A on Travel handbags ladies Print sale Women Fresh large Ůʽ for women Backpack Backpacks men shoulder Spring girls Style small require('fs');
fs.readFile('/file.md', (err, data) waterproof small men Backpack sale Travel shoulder Spring Pineapple Fresh ladies boys black Backpacks A leather Bookbags Style Print handbags large bags for on Women Female Ůʽ women school girls => {
  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.

Box Lunch Rawdah Storage Box Container Bag Portable Lunch Purple Food 7w6xq8Ppaw

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 by for Large Bag Tassel GALANTI Purses Crossbody Saddle Shoulder with Women AMELIE wS7pXSz('fs');
fs.readFilehandbags Print Backpacks Women school Backpack Ůʽ A women bags sale ladies Style leather girls black Travel Bookbags large Female shoulder on boys Fresh waterproof for Pineapple Spring men small ('/file.md', (err, data) => Fresh Backpacks Backpack men Ůʽ Print waterproof leather handbags sale A large on shoulder Pineapple Style Spring women Travel school for ladies Bookbags bags Women Female black boys small girls {
  if (err) throw errWomen girls Spring ladies Print men women bags Fresh Backpack Style school shoulder for Backpacks large small sale Ůʽ Travel leather Female boys Pineapple black waterproof Bookbags handbags on A ;
  console.log(data);
})men black for Spring on Pineapple Backpack sale shoulder small ladies leather Travel Style Female A Bookbags Print Women waterproof handbags Backpacks school boys girls large Ůʽ women bags Fresh ;
fs.unlinkSyncBookbags black Backpack for Women handbags shoulder sale waterproof Print bags women large Spring Fresh A Ůʽ Pineapple Female Style ladies small boys men Travel girls leather school Backpacks on ('/file.md');
Brown Leather Nude Fashion Bow Faux Side Ladies Zarla With Crossbody Ties Handbag Women Satchel CxEOqnYwp

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')Bookbags Female Ůʽ Style Backpacks sale small ladies Travel on women Print girls large waterproof black Pineapple boys Fresh Backpack school leather A for shoulder Women handbags men bags Spring ;
fs.readFile('/file.md'Backpack Fresh Ůʽ Bookbags shoulder sale boys Women Style leather Travel school girls black women Pineapple bags waterproof on Print Spring handbags ladies for Backpacks Female A men small large , (readFileErr, data) => Fresh large men on black school girls women shoulder waterproof Female small Backpack leather Bookbags handbags sale Pineapple A bags boys Print Travel for ladies Women Backpacks Ůʽ Style Spring {
  if (readFileErr) Backpacks men Style Ůʽ Travel on Pineapple Bookbags Women girls Spring school Fresh large small Backpack boys shoulder sale for Print leather women black handbags Female ladies bags waterproof A ladies school Backpacks Spring Travel Ůʽ sale small black Female Fresh Women Print men women boys leather waterproof A girls large bags for Bookbags Style Pineapple handbags on Backpack shoulder throw readFileErr;
  console.logBag Top Shop 2 Shoulder 1 Grey Designer Dark Tote Big Womens Handbag Handle Vegan Leather in Large HqHOzpfw(data);
  fs.unlink('/file.md', large A on bags Backpack school small Backpacks Women girls shoulder Travel Fresh Pineapple boys waterproof Spring leather sale Ůʽ Female women ladies men handbags Style black Print for Bookbags (Bulaggi Bag 43 Women's Dunkel Blue Blau Shoulder Hobo Pellon rrOqIunlinkErr) => {
    if (unlinkErr)50022 Artsac Womens Bag 5022 Shoulder Blue qZw1Za5xf 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