Skip to content

Doing I/O bound tasks in the real world

Course: Asynchronous Javascript, Callbacks and more - chapter 6

What if you were tasked with doing 3 things

  1. Boil some water.
  2. Do some laundry
  3. Send a package via mail

Would you do these

  1. One by one (synchronously)
  2. Context switch between them (Concurrently)
  3. Start all 3 tasks together, and wait for them to finish. The first one that finishes gets catered to first.

💡 Good talk - Concurrency is not parallelism - https://www.youtube.com/watch?v=oV9rvDllKEg

const fs = require("fs");
const contents = fs.readFileSync("a.txt", "utf-8");
console.log(contents);
const contents2 = fs.readFileSync("b.txt", "utf-8");
console.log(contents2);
const contents3 = fs.readFileSync("b.txt", "utf-8");
console.log(contents3);

Start all 3 tasks together, and wait for them to finish.

Section titled “Start all 3 tasks together, and wait for them to finish.”
const fs = require("fs");
fs.readFile("a.txt", "utf-8", function (err, contents) {
console.log(contents);
});
fs.readFile("b.txt", "utf-8", function (err, contents) {
console.log(contents);
});
fs.readFile("a.txt", "utf-8", function (err, contents) {
console.log(contents);
});