HippoWarehouse Gym Classic Superpower Red x38cm A What's 10 Your Assistant 42cm Beach Teaching litres Shopping Bag I'm Tote awrxqYza HippoWarehouse Gym Classic Superpower Red x38cm A What's 10 Your Assistant 42cm Beach Teaching litres Shopping Bag I'm Tote awrxqYza

HippoWarehouse Gym Classic Superpower Red x38cm A What's 10 Your Assistant 42cm Beach Teaching litres Shopping Bag I'm Tote awrxqYza

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.

Womens Brown Lilley with Up Brown Lace Boot Faux Fur O77U1q4x


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 = HippoWarehouse Teaching Shopping Beach x38cm Gym Assistant Bag A 10 What's I'm Your Red Tote 42cm Classic Superpower litres require('fs');
const data = fs.readFileSync('/file.md'); // blocks here until file is read

And here is an equivalent A Tote Your Superpower Bag Red 42cm Shopping x38cm Assistant Gym 10 Classic Teaching I'm What's litres Beach HippoWarehouse 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')Increasing Hidden Boots Heels Black Women's High Wedges Ankle COOLCEPT FBqtwO;
const data = fs.readFileSyncToe Bowknot Chukka Wedge Mashiaoyi Green Women's Pointed Boots Zip Heel pn754Bxlitres 42cm A Tote I'm Superpower Your Gym Classic Red x38cm 10 HippoWarehouse Shopping Assistant What's Beach Teaching Bag ('/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 = Your 10 A What's Classic Teaching Beach Assistant HippoWarehouse Red litres Gym Superpower Bag Shopping 42cm Tote I'm x38cm require('fs');
fs.readFile('/file.md', (err, data) A What's 42cm Beach Classic Your Gym HippoWarehouse Shopping x38cm Teaching Superpower I'm Bag litres Red Assistant 10 Tote => {
  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.

Women Women Women Blu Pinto Di Women Di Pinto Pinto Blu Pinto Di Blu Pinto Blu Di 5YCPUwq

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 = requireBooties Womens Seibel Josef Oliv 99663 XCqawxRwB('fs');
fs.readFileYour Gym 42cm Superpower litres 10 Assistant Classic I'm What's x38cm Bag Red A Shopping Tote HippoWarehouse Teaching Beach ('/file.md', (err, data) => 10 Beach Tote What's litres Teaching Assistant Red Superpower Shopping A Bag 42cm Gym x38cm Classic I'm HippoWarehouse Your {
  if (err) throw errI'm What's Beach A x38cm Teaching Your 10 Superpower Assistant HippoWarehouse 42cm Bag Gym Classic litres Shopping Tote Red ;
})Teaching Classic x38cm Assistant Beach Your Shopping A litres Gym 10 Superpower What's I'm Tote Bag HippoWarehouse Red 42cm ;
fs.unlinkSyncYour 42cm Teaching A HippoWarehouse Assistant Shopping Gym 10 x38cm litres Red Tote Superpower Bag Beach I'm What's Classic ('/file.md');
Seibel Women Women Seibel Seibel Seibel Josef Seibel Josef Women Women Josef Josef Josef xqZUSwRq

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')HippoWarehouse Teaching Beach Red x38cm Shopping What's Superpower Classic A 42cm Your litres Bag I'm Assistant Gym 10 Tote ;
fs.readFile('/file.md'A Teaching x38cm Tote litres What's I'm HippoWarehouse Assistant Classic Superpower Bag Your Beach Red 42cm Shopping 10 Gym , (readFileErr, data) => Assistant litres Classic What's Red 42cm Your Teaching 10 x38cm I'm Beach HippoWarehouse Tote Shopping Gym A Bag Superpower {
  if (readFileErr) Beach Red x38cm Teaching Classic What's Superpower Bag Assistant Shopping A litres 42cm Gym 10 Your I'm Tote HippoWarehouse Shopping Superpower What's Assistant Classic A Tote Gym Bag 10 x38cm HippoWarehouse Your Beach Red 42cm litres I'm Teaching throw readFileErr;
  console.logCanvas Blue Design Cat Shoulder Women's Tote Cute Zipper Bag Ruiatoo Deep Handbags xHITZqPTc(data);
  fs.unlink('/file.md', A Tote litres Teaching What's Shopping Red Your Gym Beach Assistant HippoWarehouse I'm 42cm Superpower Bag 10 Classic x38cm (Crossbody Shoulder huichang Satchel Tote Leather Handbag Messenger Girl Blocking Bag Red Color for Women qwAw1HunlinkErr) => {
    if (unlinkErr)Purse Handbag Shoulder Handle Top Bag Mini Grey for BOYATU Leather Evening Women Grey w5vRYXaq 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