Implementing Promise.allSettled in JavaScript
The Promise.allSettled method is a crucial part of modern JavaScript's promise handling capabilities. It allows you to track the status of multiple promises regardless of whether they resolve or reject, providing a comprehensive view of their outcomes. This challenge asks you to implement this functionality, deepening your understanding of promises and asynchronous operations.
Problem Description
Your task is to implement a Promise.allSettled function that mirrors the behavior of the native JavaScript implementation. This function should accept an iterable (typically an array) of promises as input and return a new promise. This returned promise should resolve with an array of objects, each describing the outcome of one of the input promises.
What needs to be achieved:
- Create a function named
Promise.allSettledthat accepts an iterable of promises. - The function should return a new promise.
- The returned promise should resolve with an array of objects. Each object in the array represents the outcome of a corresponding promise in the input iterable.
- Each object in the result array should have the following structure:
status: A string, either"fulfilled"or"rejected".value: If the promise was fulfilled, this property should contain the resolved value. If the promise was rejected, this property should be omitted.reason: If the promise was rejected, this property should contain the rejection reason. If the promise was fulfilled, this property should be omitted.
Key Requirements:
- The function must handle both fulfilled and rejected promises correctly.
- The function must maintain the order of the input promises in the output array.
- The function should resolve as soon as all input promises have either fulfilled or rejected.
- The function should not reject, even if any of the input promises reject.
Expected Behavior:
The returned promise should resolve with an array of objects, where each object accurately reflects the status and value/reason of the corresponding input promise. The order of the objects in the array should match the order of the promises in the input iterable.
Edge Cases to Consider:
- Empty Input: If the input iterable is empty, the function should resolve immediately with an empty array.
- Non-Promise Values: If the input iterable contains values that are not promises, they should be treated as immediately fulfilled promises with a value of the non-promise value itself.
- Promises that resolve/reject immediately: The function should handle promises that resolve or reject synchronously without issues.
Examples
Example 1:
Input: [Promise.resolve(1), Promise.reject('error'), Promise.resolve(2)]
Output: [
{ status: 'fulfilled', value: 1 },
{ status: 'rejected', reason: 'error' },
{ status: 'fulfilled', value: 2 }
]
Explanation: Each promise's outcome is captured in the result array, maintaining the original order.
Example 2:
Input: []
Output: []
Explanation: An empty input iterable results in an immediately resolved promise with an empty array.
Example 3:
Input: [Promise.resolve(1), 5, Promise.reject('error')]
Output: [
{ status: 'fulfilled', value: 1 },
{ status: 'fulfilled', value: 5 },
{ status: 'rejected', reason: 'error' }
]
Explanation: Non-promise values are treated as fulfilled promises.
Constraints
- The input iterable can contain a maximum of 100 promises.
- The values resolved or rejected by the promises can be of any JavaScript data type.
- The function should complete within a reasonable timeframe (e.g., less than 1 second) for typical inputs.
- The input iterable must be an iterable object (e.g., an array, a Set, or a Map).
Notes
- You'll need to use
Promise.resolve()to handle non-promise values in the input. - Consider using
Promise.race()or similar techniques to efficiently track the completion status of all promises. - Pay close attention to the structure of the objects in the result array – the
status,value, andreasonproperties are crucial. - Remember that
Promise.allSettledshould always resolve, even if some of the input promises reject. It provides a complete picture of all promises' outcomes.