Skip to curriculum
Jump to the editor

12.10. Merge the results of several promises

Medium
Topics
20–30 min

A settings page loads from three places at once: the app's defaults, the team's settings and the user's own. Each request returns an object, and the page needs one combined object where the user overrides the team and the team overrides the defaults. If one source fails, the page should still show what it has and say what went wrong, not fail completely.

Write mergeResults(promises) in merge-results.js. It waits for every promise, deep-merges the fulfilled objects in list order, and collects the errors.

Signature

JAVASCRIPT
function mergeResults(promises) // returns a Promise of { data, errors }
  • promises: an array of promises or plain values.
  • data: a new object, the merge of every fulfilled plain object.
  • errors: the rejection reasons, in list order.

Requirements

  1. The promise fulfills with an object holding exactly data and errors, after every input has settled. It never rejects.
  2. Fulfilled plain objects (prototype Object.prototype or null) are merged into data in list order: for the same key, the object later in the list wins, whichever finished first. Keys with the value undefined are copied like any other.
  3. When both sides hold a plain object under the same key, those objects are merged the same way, at every depth. Otherwise the later value replaces the earlier one: arrays, dates and class instances are values, never merged.
  4. Fulfilled values that are not plain objects (null, undefined, numbers, strings, arrays, class instances) are skipped.
  5. Every rejection reason goes into errors, in list order.
  6. The input objects are never changed, and data shares no nested plain object with them.
  7. A key named __proto__ is skipped at every depth, so no prototype is ever changed.
  8. An empty list fulfills with { data: {}, errors: [] }.

Examples

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

await mergeResults([
  later(300, { theme: "light", editor: { font: 14, tabs: 2 } }),
  later(100, { editor: { tabs: 4 } }),
  later(200, { theme: "dark" }),
]);
// { data: { theme: "dark", editor: { font: 14, tabs: 4 } }, errors: [] }
JAVASCRIPT
const offline = Promise.reject(new Error("team settings offline"));

await mergeResults([later(10, { theme: "light" }), offline, null]);
// { data: { theme: "light" }, errors: [Error("team settings offline")] }
JAVASCRIPT
await mergeResults([{ tags: ["a", "b"] }, { tags: ["c"] }]); // { data: { tags: ["c"] }, errors: [] }

Environment

  • Plain JavaScript in a browser page; the tests call the global mergeResults from merge-results.js.
  • The tests' promises settle on a virtual clock (setTimeout), so no test waits for real time.
  • Promise.allSettled and the other combinators are allowed. Symbol keys and getters are out of scope: copy own string keys.

Hints

  1. Which combinator waits for every promise, whatever the outcome, and keeps list order?
  2. Merging { editor: { tabs: 4 } } into { editor: { font: 14 } } is the same job one level down. What does that suggest?
  3. What does target["__proto__"] = value do to target?

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…