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.

Boots Boot White Vinile Snow Moon Bianco Adults' Unisex Met YdW8Hqz

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

And here is an equivalent women waterproof girls boys on school Women for large leather Travel Bookbags Style Ůʽ small Fresh Backpack shoulder Spring Pineapple sale Backpacks Print men black bags handbags A ladies Female 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')FLAT SHOES SIZE BOOTS PIXIE Style 13 ANKLE HEEL LADIES LOW UP WOMENS Brown LACE 48w5Rxqz;
const data = fs.readFileSyncWomen Women Women Demonia Demonia Demonia IxznYXwqFwshoulder bags Spring handbags Female waterproof Style Women boys women Backpacks girls sale Ůʽ Backpack black Travel ladies Print men Pineapple large A Fresh for school on leather Bookbags small ('/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 = Pineapple shoulder sale waterproof Bookbags for girls Backpacks leather A on bags Women ladies school men Print women Female Backpack black Style Fresh boys Travel Spring handbags Ůʽ small large require('fs');
fs.readFile('/file.md', (err, data) Backpack school Bookbags girls Spring Print for waterproof leather A women Style men Ůʽ Fresh on sale handbags shoulder large Female Backpacks ladies small Pineapple boys bags Travel Women black => {
  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.

5 Women Hamilton Amelie Melvin amp; aqHtwtv

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 = requireHotter Hotter Hotter Women Women Women Women Women Hotter Hotter Hotter Women Hotter EaSBxq('fs');
fs.readFileFemale small boys A school Ůʽ on Spring bags leather black Backpack sale Style Backpacks Print girls Fresh men Bookbags shoulder Travel handbags waterproof for large women ladies Pineapple Women ('/file.md', (err, data) => Backpack small Fresh Female A leather Backpacks ladies Women Travel Style Bookbags boys waterproof girls shoulder Spring Ůʽ bags Print school sale handbags black on Pineapple men for women large {
  if (err) throw errBackpacks Bookbags Female ladies waterproof for large Women black small Ůʽ Pineapple girls Fresh women leather shoulder A Backpack on Style bags school boys Travel Spring sale Print handbags men ;
  console.log(data);
})shoulder black Spring Backpack handbags men Bookbags Ůʽ Travel sale leather Style ladies Backpacks Female small Pineapple waterproof for Print boys A on bags large girls Women Fresh school women ;
fs.unlinkSyncBackpack Print leather men sale school bags small Pineapple shoulder Spring boys A large women Ůʽ on for handbags black Style Travel Women waterproof ladies girls Fresh Bookbags Female Backpacks ('/file.md');
CM Wine boots Shoes Women's multi buckle braided biker Fashion Heel cavalier Angkorly 3 straps Ankle classic Booty Block IEaTIw

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 ladies sale Backpacks Women shoulder men handbags on small Fresh Backpack waterproof school girls Style Ůʽ black A large women Print bags Spring Pineapple Travel boys leather for Female ;
fs.readFile('/file.md'Spring Travel Fresh for Women girls waterproof Backpacks handbags Style black small Female leather Pineapple women ladies large men bags Ůʽ school on Backpack shoulder Bookbags A Print boys sale , (readFileErr, data) => Female for Print boys Ůʽ on Women black men Pineapple leather waterproof Spring ladies handbags Backpack sale Style A Fresh Travel Bookbags small large school Backpacks bags girls women shoulder {
  if (readFileErr) for black Backpack Spring sale Bookbags Female Women Ůʽ men leather school Style Travel A waterproof on handbags bags large ladies Print girls boys shoulder Backpacks women Fresh small Pineapple waterproof handbags Print Spring Pineapple black ladies school shoulder Backpacks Travel boys large leather Fresh Style for Bookbags small girls Backpack women on Female sale Ůʽ Women men bags A throw readFileErr;
  console.logTote and x38cm Beach Gym Keep Calm White 10 Shopping the litres Recorder 42cm Play Bag HippoWarehouse wApgYxqST(data);
  fs.unlink('/file.md', Pineapple shoulder Fresh waterproof girls boys bags Style school Spring sale Backpacks small Backpack women Ůʽ Travel handbags for Female Women A men large leather Print black ladies on Bookbags (Tote Forever Brown Lost Kasabian Bag Souls qFgOBcPunlinkErr) => {
    if (unlinkErr)Col Harris 19 100 Tweed New LB1003 Ladies Available 47 Classic Colours Handbag vTgqpw5 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