Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
12.8. Print results in order as soon as each is ready
Medium
Topics
This question is in
12. Callbacks, promises and asyncA 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
function printInOrder(promises, onValue, onError) // returns a Promise of undefinedpromises: 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
- Results are reported in input order:
onValueoronErrorfor index 0, then index 1, and so on, exactly once each. - Result
iis reported at the moment promiseiand all the promises before it have settled, not later. Nothing waits for promises after it. - A rejection is reported with
onError(reason, i)in its place, and the reports after it carry on. - No rejection is ever left unhandled, even when a later promise rejects long before its turn.
- Plain values count as already settled. An empty array reports nothing.
- The returned promise fulfills with
undefinedafter the last report, and never rejects. - The input array is not changed.
Examples
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)printInOrder([later(300, "A"), later(100, "B")], console.log, console.error);
// at 300 ms: "A" 0, then "B" 1const 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" 2Environment
- Plain JavaScript in a browser page; the tests call the global
printInOrderfromprint-in-order.js. - The tests' promises settle on a virtual clock (
setTimeout), so no test waits for real time. Promise.alldoes not help: it waits for every promise before giving anything.
Hints
- Report
ineeds two things to have happened. Which promise could stand for "reporti - 1is done"? - 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…