Middle
/ ** * Implement the function sumPromises, which takes * promises as arguments and returns the sum * of their resolved results. * * The function can accept any number of arguments. * You can use any Promise APIs. */ // Code here // Example usage const promise1 = Promise.resolve(1); const promise2 = Promise.resolve(2); sumPromises(promise1, promise2).then(console.log); // 3
sobes.tech AI
Answer from AI
javascript function sumPromises(...promises) { return Promise.all(promises) .then(results => results.reduce((sum, value) => sum + value, 0)); }
// Example usage const promise1 = Promise.resolve(1); const promise2 = Promise.resolve(2);
sumPromises(promise1, promise2).then(console.log); // 3
The function takes any number of promises, waits for their resolution using `Promise.all`, and then sums the obtained values.