Skip to curriculum
Jump to the editor

12.8. Print results in order as soon as each is ready

Medium
Topics
20–30 min

A chat app loads the last five messages of a thread with five requests at once. They come back in any order, but the messages must appear in order, oldest first. Waiting for all five before showing anything wastes time: if the first message is ready, it can be shown right away. The rule is: show message i as soon as it and every message before it have arrived. A slow first message holds the others back; a slow last one holds back nothing.

Write printInOrder(promises, onValue, onError) in print-in-order.js.

Signature

JAVASCRIPT
function printInOrder(promises, onValue, onError) // returns a Promise of undefined
  • promises: an array of promises or plain values, all already started.
  • onValue(value, index): report a fulfilled result.
  • onError(reason, index): report a rejected one.

Requirements

  1. Results are reported in input order: onValue or onError for index 0, then index 1, and so on, exactly once each.
  2. Result i is reported at the moment promise i and all the promises before it have settled, not later. Nothing waits for promises after it.
  3. A rejection is reported with onError(reason, i) in its place, and the reports after it carry on.
  4. No rejection is ever left unhandled, even when a later promise rejects long before its turn.
  5. Plain values count as already settled. An empty array reports nothing.
  6. The returned promise fulfills with undefined after the last report, and never rejects.
  7. The input array is not changed.

Examples

JAVASCRIPT
const later = (ms, value) => new Promise((resolve) => setTimeout(() => resolve(value), ms));

printInOrder([later(100, "A"), later(300, "B"), later(200, "C")], console.log, console.error);
// at 100 ms: "A" 0
// at 300 ms: "B" 1, then "C" 2 (C was ready at 200 but waited for B)
JAVASCRIPT
printInOrder([later(300, "A"), later(100, "B")], console.log, console.error);
// at 300 ms: "A" 0, then "B" 1
JAVASCRIPT
const failed = Promise.reject(new Error("deleted"));

await printInOrder([later(50, "A"), failed, "C"], console.log, (error, i) => console.log("error", error.message, i));
// at 50 ms: "A" 0, "error deleted" 1, "C" 2

Environment

  • Plain JavaScript in a browser page; the tests call the global printInOrder from print-in-order.js.
  • The tests' promises settle on a virtual clock (setTimeout), so no test waits for real time.
  • Promise.all does not help: it waits for every promise before giving anything.

Hints

  1. Report i needs two things to have happened. Which promise could stand for "report i - 1 is done"?
  2. When does the browser decide that a rejection is unhandled? When does your code attach its handler to promise 4?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…