Loading video player…

Watch a Promise Settle

In this lesson, you logged the Adeyemi family’s picksPromise twice, once straight away and once from a timer, and watched it go from pending to fulfilled.

Key Points

  • readFile() from node:fs/promises gives back a promise straight away. It’s a placeholder for the text of picks.txt, not the text itself, so logging it right away shows Promise { <pending> }. Passing a callback to .then() hands you the text, but it doesn’t show you the promise itself.
  • To look at the same promise later, a setTimeout() callback can log it again. The script runs on the call stack to its end, while Node reads the file and the timer waits, outside your code, in the waiting area. When the timer is due, its callback joins the task queue, and the event loop moves it onto the stack once the stack is empty.
  • In this run, Node finished reading long before the 100-millisecond timer was due. It took a short turn on the empty stack, put the text into the promise, and changed its state from pending to fulfilled. Node makes that change itself, once the read finishes. How Node hands the text over comes later in the course.
  • A promise keeps its value, however late you look. Node shows a fulfilled promise’s text as a string, in quotes, with \n for each line break.
  • If the script asked for a file that isn’t there, like Folake’s old_picks.txt, the read would fail, and the promise would be rejected instead, holding the reason, an error saying there’s no such file.
  • Fulfilled or rejected, the promise is settled, and it settles only once. It never goes back to pending, and it never switches between fulfilled and rejected.

The rule: a promise starts pending. It settles at most once, as fulfilled or rejected, and then it keeps its value.

The Final Code

The family’s picks, as you saved them in the last lesson:

Language: Text Filename: picks.txt
Tolu: 28560, 111628
Grandma Bisi: 27992
Folake: 6565, 20684
Tunde: 16568

The script logs the promise straight away, then again from a timer 100 milliseconds later:

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

const picksPromise = readFile("picks.txt", "utf8");
console.log(picksPromise);

setTimeout(() => {
  console.log(picksPromise);
}, 100);

Run it with Node, and the same promise shows up twice, first pending, then holding the text. That’s this run’s output. On a slower disk, the read could still be pending when the timer looks.

Language: Shell
$ node watch_picks.mjs
Promise { <pending> }
Promise {
  'Tolu: 28560, 111628\nGrandma Bisi: 27992\nFolake: 6565, 20684\nTunde: 16568\n'
}

A timer only lets you peek. Real code can’t rely on how long a read takes. Next, in Handle Results With .then() and .catch(), you’ll take a proper look at .then(), and at .catch() for a rejection.

00:00 In the last lesson, the Adeyemi family’s planning script read picks.txt and logged what readFile() gave back. It logged a promise, still pending.

00:11 That promise is a placeholder for the family’s picks, not the picks themselves.

00:17 Then you passed a callback to .then(), and it handed you the text. But you never looked at the promise itself again. What does it show once the file has loaded?

00:27 In this video, you’ll look again, and watch the promise settle.

00:33 To look later, you need a line that runs later. A timer can do that, and you’ve used one before. The new script, watch_picks.mjs, starts with lines you know.

00:47 It reads picks.txt into picksPromise, and logs the promise straight away. Then it calls setTimeout() with a delay of 100 milliseconds. The callback logs the same promise again.

01:02 Here’s where each part waits. The script runs on the call stack, to its end. Node reads the file outside your code, in the waiting area. The timer waits there too.

01:14 When it’s due, its callback joins the task queue, and the event loop moves it onto the stack once the stack is empty. The first log will show the promise pending, as before.

01:26 Your turn: pause the video and predict what the second log will show.

01:36 In this run, the second log shows the same promise, now holding the text of the file. Node shows the text the way you’d write it as a string, in quotes, with a \n for each line break.

01:49 So what changed in those 100 milliseconds? Node finished reading long before the timer was due. It took a short turn on the empty stack, put the text into the promise, and changed its state from pending to fulfilled.

02:04 How Node hands the result over comes later in the course. Then the timer was due. Its callback ran on the empty stack, and logged the promise again, with the text still there.

02:16 A promise keeps its value, however late you look.

02:21 Fulfilled is one of two ways a pending promise can go. If the script asked for a file that isn’t there, like Folake’s old_picks.txt, the read would fail, and the promise would be rejected instead.

02:35 It would hold the reason, an error saying there’s no such file.

02:40 Fulfilled or rejected, the promise is now settled. And a promise settles only once. There’s no arrow back to pending, and none between fulfilled and rejected, either way.

02:53 Here’s the rule. A promise starts pending. It settles at most once, as fulfilled or rejected, and then it keeps its value.

03:04 A timer only lets you peek, though. On a slower disk, the read could still be pending when the timer looks. Real code can’t rely on how long a read takes. Your script needs the picks whenever they arrive.

03:17 Next, in Handle Results With .then() and .catch(), you’ll take a proper look at .then(), and at .catch() for a rejection.

Become a Member to join the conversation.