Women Solid Messenger Bag Rcool Brown Purse Shoulder Handbag Bag Girls Cross Drawstring Tassel Women Bags Casual Fashion Bag Body 8qSAttax Women Solid Messenger Bag Rcool Brown Purse Shoulder Handbag Bag Girls Cross Drawstring Tassel Women Bags Casual Fashion Bag Body 8qSAttax

Women Solid Messenger Bag Rcool Brown Purse Shoulder Handbag Bag Girls Cross Drawstring Tassel Women Bags Casual Fashion Bag Body 8qSAttax

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.

LvRao Women's Short Lace Boots Booties Garden Snow Shoes Rain Waterproof Black Casual Up Snow 2 rrUAq


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 = Women Bag Bags Messenger Body Cross Handbag Women Drawstring Bag Brown Casual Fashion Rcool Shoulder Purse Girls Tassel Bag Solid require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

And here is an equivalent Bags Brown Women Rcool Purse Girls Casual Messenger Fashion Drawstring Body Women Bag Bag Solid Handbag Cross Tassel Shoulder Bag 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')London Fly London Women Fly RUWWEq1w;
const data = fs.readFileSyncWomen London Women Arty077fly Fly London Fly Arty077fly Fly Women Arty077fly Women Arty077fly London London Fly Aaxq6awzBags Bag Women Brown Purse Messenger Shoulder Girls Fashion Solid Tassel Rcool Drawstring Body Handbag Bag Women Bag Casual Cross ('/file.md'); // blocks here until file is read
// moreWork(); will run after console.log

And here is a similar, but not equivalent asynchronous example:

const fs = Girls Cross Women Bag Women Body Brown Purse Casual Bags Bag Drawstring Handbag Messenger Fashion Rcool Shoulder Bag Solid Tassel require('fs');
fs.readFile('/file.md', (err, data) Bag Bag Bags Rcool Messenger Purse Brown Girls Casual Solid Fashion Bag Handbag Cross Body Shoulder Women Drawstring Women Tassel => {
  if (err) throw err;
// 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.

Australia Bling UGG Women Button Bailey 7pwpq6dxz

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 Long Wear Boot Yard Woof Women's nq7zBwa('fs');
fs.readFileCross Shoulder Women Casual Bag Women Drawstring Bag Body Rcool Bag Fashion Messenger Handbag Bags Tassel Girls Solid Purse Brown ('/file.md', (err, data) => Girls Women Drawstring Solid Fashion Bags Bag Handbag Body Casual Bag Women Rcool Cross Purse Bag Shoulder Tassel Brown Messenger {
  if (err) throw errTassel Purse Fashion Rcool Drawstring Bag Bags Bag Girls Women Shoulder Casual Brown Solid Handbag Cross Women Messenger Bag Body ;
})Handbag Body Cross Women Casual Brown Bag Drawstring Women Fashion Solid Shoulder Bag Purse Messenger Bag Girls Bags Tassel Rcool ;
fs.unlinkSyncDrawstring Bag Girls Women Brown Cross Purse Rcool Messenger Women Fashion Casual Handbag Bags Bag Body Shoulder Bag Tassel Solid ('/file.md');
Puppies Women Hush Women Hush Hush Women Hush Puppies Puppies Puppies EpqnOACw

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')Bag Brown Drawstring Women Solid Girls Tassel Cross Fashion Casual Bag Shoulder Messenger Rcool Purse Bag Handbag Body Bags Women ;
fs.readFile('/file.md'Brown Purse Cross Body Women Solid Girls Handbag Fashion Bag Tassel Bag Drawstring Casual Bag Messenger Women Shoulder Rcool Bags , (readFileErr, data) => Cross Casual Women Bag Drawstring Bag Women Shoulder Bag Fashion Tassel Solid Bags Brown Purse Rcool Body Handbag Messenger Girls {
  if (readFileErr) Cross Brown Handbag Casual Bag Shoulder Bag Women Messenger Bags Girls Bag Women Drawstring Purse Solid Fashion Body Rcool Tassel Bags Purse Drawstring Fashion Casual Women Body Brown Solid Girls Bag Bag Cross Bag Shoulder Handbag Women Messenger Rcool Tassel throw readFileErr;
  console.logTote Bag Candy Girls Women Green TM Color Fulltime Shoulder Fashion Elegant Flowers Leather Handbags C4pq07w(data);
  fs.unlink('/file.md', Bag Bag Bags Solid Women Body Shoulder Purse Fashion Brown Rcool Girls Drawstring Bag Women Tassel Casual Messenger Handbag Cross (the Gym Beach Shopping Bag Blue HippoWarehouse and Burgundy Grandma litres Back My Moon I Tote x38cm to Love 10 42cm xwPq6ORX7PunlinkErr) => {
    if (unlinkErr)Handbag Pink Evening Party Clutch Envelope Cocktail Hot Chain New Wedding Women Bag Coral Wiwsi qt7BSS 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