Combine Independent Results
Combine Independent Results
Promise.all gathers results in input order when every input fulfills. The school can request independent subject marks together, then calculate a total after both results arrive.
Before this topic, review Trace Tasks and Promise Handlers if its terms are unfamiliar.
What you’ll learn
- Explain combine independent results through the school example.
- Trace the shown operations and distinguish their result from displayed output.
- Make the practice change and explain its consequence: Both marks still contribute; the second rises by 5.
Work through the school example
This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.
JavaScript · school.js · complete example
Promise.all([Promise.resolve(35), Promise.resolve(40)]).then((marks) => console.log(marks.reduce((sum, mark) => sum + mark, 0)));
Expected output
75Run in JavaScript compiler →Recognize the important boundary
all rejects on an input rejection but does not cancel other work. allSettled records every outcome; race and any select by different settlement rules.
Try a small variation
JavaScript · original source for practice
Promise.all([Promise.resolve(35), Promise.resolve(40)]).then((marks) => console.log(marks.reduce((sum, mark) => sum + mark, 0)));
Your task: Replace resolve(40) with resolve(45). Predict the result before running the edited program.
The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.
Worked answer and reasoning
Both marks still contribute; the second rises by 5.
JavaScript · complete worked variation
Promise.all([Promise.resolve(35), Promise.resolve(45)]).then((marks) => console.log(marks.reduce((sum, mark) => sum + mark, 0)));
Expected output
80Run in JavaScript compiler →Predict the original program
JavaScript · original program for this question
Promise.all([Promise.resolve(35), Promise.resolve(40)]).then((marks) => console.log(marks.reduce((sum, mark) => sum + mark, 0)));
Keep the useful idea
Promise.all gathers results in input order when every input fulfills. The school can request independent subject marks together, then calculate a total after both results arrive.
Check your variation against this explanation: Both marks still contribute; the second rises by 5.
Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.
Reaching the recap and completing the quizzes are separate milestones.