Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Reading a Whole File

Reading a Whole File

Dev’s port has the log’s name from the command line, and now it needs to read the log. Dev’s Python tool loops over the log, but the port reads the whole text first, which in Python is text = f.read() inside with open(path, encoding="utf-8") as f:.

Python’s open() is built in, but in JavaScript the port imports readFile() from Node’s built-in file module, node:fs/promises, and the path line ports as before. One call takes the path and "utf8", like Python’s encoding, with no file object and no with block. Here’s the line-by-line port. What does it log when it runs?

Language: JavaScript Filename: digest.js
import { readFile } from "node:fs/promises";

const [path] = process.argv.slice(2);
const text = readFile(path, "utf8");
console.log(text);
const lines = text.split("\n");

Run as node digest.js logs/week-44.log, it logs a pending promise, not the text, and then the last line throws (your terminal also shows Node’s source line and stack trace, left out here):

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Dev’s port has the log’s name from the command line. Now you’ll read the log with one call, and find out what that call really hands back. Dev’s Python tool loops over the log, but the port reads the whole text first: in Python, f.read() inside a with block.

00:19 Python’s open() is built in, but in JavaScript the port imports readFile() from Node’s built-in file module, and the path line ports as before.

00:29 One call takes the path and "utf8", like Python’s encoding, with no file object and no with block. The last two lines port line for line.

00:40 What does the port log when you run it? Your turn: pause the video and predict.

00:51 It logs a pending promise, not the text. Then the last line throws a type error: .split() is not a function. A promise isn’t a string, so it has no .split().

01:05 Python’s f.read() waits for the file. readFile() doesn’t: reading takes time, so it hands back a promise of the text straight away, pending until the file is read.

01:17 await waits for that promise and gives you its text. How promises work comes later. For now, put await before the call. In a module, await works at the top level, outside any function, so the script still reads top to bottom.

01:34 Run it again, and it logs the ten readings, with no error from the last line. Watch out: without "utf8", readFile() gives a buffer of bytes, as Python’s open() does only in "rb" mode, and .split() fails the same way, so always pass it for text.

01:53 On week forty-five, whose log Dev hasn’t copied yet, the port stops, as Python’s would, with an error whose code is ENOENT: error, no entry. Handling it comes later.

02:09 Inside a function, like this readLog() in readings.js, which Asha can reuse, await needs async in front of the word function.

02:18 Even though readLog() returns the text, calling it hands back a promise of that text, so whoever calls it awaits it in turn. Take it as a recipe for now.

02:30 That’s the JavaScript way: one awaited call reads the whole file, with "utf8" for text, and there’s no file object to close. Next up is Splitting a File Into Lines, a text lesson, where you’ll port Python’s most common file loop.

Become a Member to join the conversation.