promiseSettle must returns a Promise
that is fulfilled when all promises in input
are settled (meaning, either resolved or rejected).
The fulfillment value will be an array of objects, each having the following signature:
- @typedef {Object} Settlement
- @property {boolean} isFulfilled - whether the promise resolved
- @property {boolean} isRejected - whether the promise rejected
- @property {*=} value - the value (if any) with which the promise was resolved
- @property {*=} reason - the reason (if any) with which the promise was rejected
- @param {Array.>} input - an array of Promises
@return {Promise.>}
function promiseSettle(input) { let promiseArray = []; for (let i = 0; i < input.length; i++) { Promise.resolve(input[i]).then(output => { promiseArray.push(output); console.log(promiseArray); }, reason => { promiseArray.push(reason); }) } } // testing data var p1 = new Promise((resolve, reject) => { setTimeout(reject, 1, "first promise of 1 sec"); }); var p2 = new Promise((resolve, reject) => { setTimeout(resolve, 1, "second promise of 2 sec"); }) var p3 = new Promise((resolve, reject) => { setTimeout(resolve, 1, "rejected promise"); }) promiseSettle([p1, p2, p3])
Can someone help with this? I am not sure how to return the promise with the expected parameters.