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
- Boil some water.
- Do some laundry
- Send a package via mail
Would you do these
- One by one (synchronously)
- Context switch between them (Concurrently)
- 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
Synchronously (One by one)
Section titled “Synchronously (One by one)”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);});