Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
12.10. Merge the results of several promises
Medium
Topics
This question is in
12. Callbacks, promises and asyncA 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
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
- The promise fulfills with an object holding exactly
dataanderrors, after every input has settled. It never rejects. - Fulfilled plain objects (prototype
Object.prototypeornull) are merged intodatain list order: for the same key, the object later in the list wins, whichever finished first. Keys with the valueundefinedare copied like any other. - 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.
- Fulfilled values that are not plain objects (
null,undefined, numbers, strings, arrays, class instances) are skipped. - Every rejection reason goes into
errors, in list order. - The input objects are never changed, and
datashares no nested plain object with them. - A key named
__proto__is skipped at every depth, so no prototype is ever changed. - An empty list fulfills with
{ data: {}, errors: [] }.
Examples
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: [] }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")] }await mergeResults([{ tags: ["a", "b"] }, { tags: ["c"] }]); // { data: { tags: ["c"] }, errors: [] }Environment
- Plain JavaScript in a browser page; the tests call the global
mergeResultsfrommerge-results.js. - The tests' promises settle on a virtual clock (
setTimeout), so no test waits for real time. Promise.allSettledand the other combinators are allowed. Symbol keys and getters are out of scope: copy own string keys.
Hints
- Which combinator waits for every promise, whatever the outcome, and keeps list order?
- Merging
{ editor: { tabs: 4 } }into{ editor: { font: 14 } }is the same job one level down. What does that suggest? - What does
target["__proto__"] = valuedo totarget?
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…