FDE Times dataFDE jobs open 922▼2 Change in open roles over 7 days, like-for-like: roles from companies we started tracking this week are left outNew in 7 days 64Companies hiring 201Remote-friendly 23%Median US pay $202kTop hirer Databricks 113
中文VI

The newspaper of the Forward Deployed Engineer

Books & courses

Why backend engineers can skip to chapter 8 of Eloquent JavaScript

A script that delivers 960 orders but reports just one runs through four chapters on the skills that decide whether an integration works.

Cover of Eloquent JavaScript
Eloquent JavaScript · Marijn Haverbeke · Cover: Open Library
ShareLinkedInFacebookX

In brief

  • Backend engineers who know the syntax should read chapters 8, 11, 18 and 20; those less sure of it should read the first 12 chapters first.
  • fetch still resolves when the server returns an error code, so check the response before counting a success.
  • Every await is a gap: don't let multiple tasks add to the same variable across it.
Bar chart: for the same 1,000 orders, the initial script reports 1 success; after the Chapter 11 fix it reports 1,000, including 40 that failed with a 500 error; after the Chapter 18 response.ok check it reports 960, matching the number of orders the system actually received.
Fixing the counter only takes the figure from 1 to 1,000; checking response.ok yields 960, exactly the number of orders the receiving system got. Source: illustrative example in the article.

The server returns an error code, yet the fetch promise resolves as if all were well. That trap appears in chapter 18 of Eloquent JavaScript, and it can turn a smooth integration demo into a data store with missing records.

For an FDE, JavaScript can sit at both ends of an integration. OpenAI’s FDE opening in Seoul asks candidates to write and review production-grade code across frontend and backend, in Python, JavaScript or a comparable stack.

The web version of Eloquent JavaScript is in its 4th edition (2024): 12 chapters on the language, seven on the browser and two on Node.js.

Its author, Marijn Haverbeke, also built Acorn, a JavaScript parsing library, so he is worth listening to on how the language works. Chapter numbers below follow the web version; the No Starch Press print edition has one extra chapter.

Where to start depends on how much JavaScript you have. Backend engineers who can already read functions, objects and basic async/await can go straight to chapters 8, 11, 18 and 20, in that order. Those less sure of the syntax should read all of the first 12 chapters before moving on to 18 and 20.

The four chapters suit the first group because they teach debugging, waiting on slow tasks, calling APIs and receiving requests, the work that decides whether an integration is right or wrong. The rest of this article follows that route.

Chapter 8: form a hypothesis before you fix

One hypothetical example runs through all four chapters. A script pushes 1,000 orders to a client’s API; the system at the other end accepts 960 and returns a 500 error for 40. The script reports just one success:

async function send(order) {
  await fetch(url, {
    method: 'POST',
    body: JSON.stringify(order)
  });
  return 1;
}

let ok = 0;
await Promise.all(orders.map(async (order) => {
  ok += await send(order);
}));

The tempting reflex is to add retries, raise the timeout and run it again. Haverbeke advises the opposite: stop and think, analyze what is happening and come up with a theory of the cause. Here, the client’s logs show that 960 orders arrived, so the first hypothesis to test is that the counter is wrong.

The chapter also separates errors that can be expected in operation from those made by the programmer. With the latter, he argues, letting the error through is usually the best course. An overloaded API needs its own handling; a mistyped field name should break the program at once, rather than be swallowed by a broad catch block.

Your first job at the client is to draw up those two lists of errors with the person who operates the system, before writing a line of retry code. For commands that write data, ask one more question: does resending the same order create a duplicate?

Chapter 11: why adding 1,000 times gives 1

Asynchronous programming lets a program wait on a long task without freezing. The price, Haverbeke warns, is that execution may contain gaps in which other code can run.

The line ok += await send(order) falls into that gap. Each task reads ok while it is still 0, pauses at await, then wakes and writes 0 plus 1. All 1,000 tasks write the same number in turn, and the final result is 1.

A neat fix is to have each task return its own result, wait for them all, then add them up:

const results = await Promise.all(orders.map(send));
const ok = results.reduce((sum, n) => sum + n, 0);

Now the script reports 1,000. The number looks good, and it is still wrong. Before going further, check every shared variable that is read before an await and written after it; on a real project, you should also limit the number of requests running at once.

Chapter 18: from 1,000 down to 960

The 40-order discrepancy comes from the trap described at the start. Because fetch resolves even with an error code, send returns 1 for every response, 500 errors included. Keep the response and check it before counting a success:

async function send(order) {
  const response = await fetch(url, {
    method: 'POST',
    body: JSON.stringify(order)
  });
  return response.ok ? 1 : 0;
}

With the same data, the total is now 960. But if one request loses its connection, fetch rejects, Promise.all rejects with it and the script loses the total altogether, even though many orders have reached the client’s system.

A dropped connection belongs on the list of operational errors you drew up in chapter 8, so catch it separately. The try block wraps only the fetch call, so programmer errors elsewhere still surface:

async function send(order) {
  const body = JSON.stringify(order);
  let response;
  try {
    response = await fetch(url, { method: 'POST', body });
  } catch (error) {
    return { id: order.id, state: 'unknown', error };
  }
  return {
    id: order.id,
    state: response.ok ? 'ok' : 'failed',
    status: response.status
  };
}

const results = await Promise.all(orders.map(send));
const ok = results.filter((r) => r.state === 'ok').length;

Say one of the 960 orders loses its connection: the script reports 959 ok, 40 failed and 1 unknown, which still adds up to 1,000. The unknown order may or may not have gone in, because the connection can drop after the server has finished writing.

Look up the order ID in the target system before resending, and read error: if all 1,000 orders are unknown, suspect a wrong URL before blaming the network.

At the end of each run, reconcile these three numbers with the record count in the target system, then give the client a list of the order IDs that need attention.

The same chapter explains why an API can work from the terminal and fail in the browser. By default, browsers block scripts from making requests to other domains; the server at the other end has to allow them through the Access-Control-Allow-Origin header. When you hit a CORS error, ask who manages that server before adding more frontend code.

Chapter 20, and what to leave for later

Chapter 20 moves on to Node.js, with modules, NPM, the file system, HTTP, streams and a file server project. Haverbeke’s reminder is one to keep: as soon as a program accepts requests over a network, it has to worry about security.

In the 1,000-order example, this is where you move to the receiving side. Build a small endpoint in a file called server.mjs, and write the rejection code before the code that stores orders:

import { createServer } from 'node:http';

const token = process.env.API_TOKEN;
if (!token) throw new Error('API_TOKEN is missing');

createServer((request, response) => {
  if (request.headers.authorization !== `Bearer ${token}`) {
    response.writeHead(401).end();
    return;
  }
  response.writeHead(501).end(); // no success until the order is stored
}).listen(8000);

The chapter 18 script does not send a token, so when pointed at this server over a stable connection it reports 0 ok and 1,000 failed, all with status 401. Retrying is useless, because the fault lies in access rights. Before replacing the 501 line with code that stores orders, ask the client who may call the endpoint and which data must be rejected.

Among those who know the syntax, anyone writing Node.js daily can start at chapters 11 and 18, and use chapter 8 to check their habits. The remaining browser chapters can wait until a project needs an interface.

Turn the exercise into evidence on your CV

In the book’s opening pages, Haverbeke writes that it takes practice to come to recognize what a good program looks like; a list of rules does not give you that feel. A CV line saying “read Eloquent JavaScript” therefore carries little weight.

A repo with a mock server, the sync script, tests for 40 failed orders and one dropped request, plus a README explaining the three numbers 1, 1,000 and 960, shows a recruiter how you think.

When a job ad asks for both frontend and backend, as OpenAI’s does in Seoul, be ready to tell the story of this bug: the hypothesis, the test, the fix and the reconciliation. Then, the next time a screen reports that every record succeeded, you will know what to ask first: who counted, and how?

Was this article useful?

Use with your AI assistantAsk Claude ↗Ask ChatGPT ↗
8 sources
Read next on the roadmap · Stage 1: FoundationsThe free security course FDEs should take before handing over codeThe most useful parts of LFD121 are a few small habits around secrets, logs, SBOMs and a public channel for reporting vulnerabilities.