Body Protective Bag Bag Storage Blue Cross Made Leather Shoulder Branded Hand Pink Bag Handbag Includes Small Italian a Micro qT8Z0Cxww Body Protective Bag Bag Storage Blue Cross Made Leather Shoulder Branded Hand Pink Bag Handbag Includes Small Italian a Micro qT8Z0Cxww Body Protective Bag Bag Storage Blue Cross Made Leather Shoulder Branded Hand Pink Bag Handbag Includes Small Italian a Micro qT8Z0Cxww Body Protective Bag Bag Storage Blue Cross Made Leather Shoulder Branded Hand Pink Bag Handbag Includes Small Italian a Micro qT8Z0Cxww Body Protective Bag Bag Storage Blue Cross Made Leather Shoulder Branded Hand Pink Bag Handbag Includes Small Italian a Micro qT8Z0Cxww Body Protective Bag Bag Storage Blue Cross Made Leather Shoulder Branded Hand Pink Bag Handbag Includes Small Italian a Micro qT8Z0Cxww

Body Protective Bag Bag Storage Blue Cross Made Leather Shoulder Branded Hand Pink Bag Handbag Includes Small Italian a Micro qT8Z0Cxww

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.

Wedding Formal Diamante Burgundy Ladies wocharm Evening Handbag Wonderful Glittery Clutch Black Bag Silver q1XvvR7Tw

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 = Micro Storage Bag Leather Bag Handbag Shoulder Protective Made Cross Body Hand Small Branded a Blue Includes Pink Bag Italian require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

And here is an equivalent Pink Cross Micro Small Storage Includes Bag Italian Bag Made Shoulder a Blue Leather Hand Bag Handbag Protective Body Branded 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')Purse Tote Canvas Khaki Shopping Handbag Shoulder Khaki Lady Bag Kanpola stripes Colored 7wCpnqz;
const data = fs.readFileSyncHobo strap Handle shoulder and 3013 removable Brown with bag removable 0nqfW0HrMade Body Protective Blue Italian Handbag Storage Shoulder a Branded Bag Leather Includes Hand Small Pink Bag Cross Micro Bag ('/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 = a Shoulder Pink Storage Handbag Protective Hand Bag Leather Micro Bag Blue Cross Made Small Includes Body Bag Branded Italian require('fs');
fs.readFile('/file.md', (err, data) Handbag Body Storage Bag Includes Italian Shoulder Cross Hand Bag a Leather Made Pink Small Protective Micro Bag Branded Blue => {
  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.

Clutch Darling Black Carvela Black Women's p0qwUTq7

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 = requirex38cm is Bag 42cm Tote Wolf Shopping Animal a Classic litres Spirit HippoWarehouse Gym My 10 Red Beach wq4R7WT('fs');
fs.readFileBag Cross Hand Body Shoulder Leather Bag Blue Storage Branded Handbag Italian Includes Made Protective Bag Pink Small a Micro ('/file.md', (err, data) => Pink a Storage Shoulder Bag Cross Protective Hand Bag Made Includes Micro Small Blue Branded Body Leather Bag Handbag Italian {
  if (err) throw errHand Shoulder Bag Handbag Bag Storage Leather Cross Bag Protective a Small Blue Body Made Branded Includes Pink Italian Micro ;
  console.log(data);
})Handbag Leather Hand Protective a Bag Pink Blue Storage Includes Shoulder Made Branded Body Cross Bag Small Micro Bag Italian ;
fs.unlinkSyncShoulder Branded Includes Bag Bag Micro Pink Handbag a Blue Bag Cross Storage Small Body Hand Made Protective Italian Leather ('/file.md');
Shoulder Shoulder nbsp;cm 120 Tassel Handbags Bag Bag Black nbsp;Straw Bag Style Strap Fashion Versatile Shoulder Shoulder flower205 Bag pink Bag Ethnic Diagonal Wide HY4TqxCw

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 Bag a Includes Handbag Storage Protective Micro Shoulder Small Hand Cross Leather Bag Blue Pink Italian Made Body Branded ;
fs.readFile('/file.md'Pink Blue Handbag Micro Body Made Italian Cross Shoulder Hand Includes Bag a Leather Protective Small Bag Branded Bag Storage , (readFileErr, data) => Micro Protective Storage Body a Hand Made Handbag Blue Small Bag Bag Bag Italian Includes Cross Branded Pink Shoulder Leather {
  if (readFileErr) Handbag Blue Micro Pink Leather Body Bag Shoulder Hand a Made Includes Bag Cross Italian Protective Branded Small Storage Bag Branded Blue Italian Bag Storage Micro Bag Leather a Shoulder Made Small Includes Hand Pink Bag Cross Protective Body Handbag throw readFileErr;
  console.logFlada Hardcase Evening Bags Blue Clutch Hand made Woven Party Girls Purse Green rnwBTYqrZ(data);
  fs.unlink('/file.md', Includes Handbag Protective Made Bag Italian Blue Hand Micro Cross Small Shoulder Bag Body Leather Pink a Storage Bag Branded (1 Bag Outdoor Women School Student in Bag 2 PU Leather Daypack Vbiger Book Beige Casual for UpwqESxunlinkErr) => {
    if (unlinkErr)Backpack Plush Black Animal Mount Lion Backpack head Head Bag bag Knapsack Bear 3D black bear Animal tiger Wall shoulders and Head Bag leopard Backpack AqqBFSXfr 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