Middle+
/** * Implement the sumPromises function that takes * promises as arguments and returns * the sum of their results. * * The function can accept any number of arguments. * You can use any Promise API. */ // 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 accepts any number of promises, waits for their completion using `Promise.all`, and then sums the results.