I've seen this question floating around so I wasn't totally blindsided, but actually writing it cleanly under pressure is a different story.
Start by clarifying the requirements and edge cases (empty input, non-promise values, rejection handling, order preservation). Then implement a function that returns a new promise, iterates over the input, and resolves with an array of results in the original order, rejecting immediately on the first rejection. Finally, discuss trade-offs and potential optimizations.
Pro tip: Mention that native Promise.all uses a counter to track pending promises and resolves only when all are settled, and that it handles thenables by wrapping them with Promise.resolve. This shows deep understanding beyond a naive implementation.
Ask about input types (iterable, array), handling of non-promise values, empty input, and rejection behavior. Confirm that order of results must match input order.
Outline a plan: return a new Promise; if input is empty, resolve immediately; otherwise, maintain a results array and a counter of resolved promises. For each input, wrap it in Promise.resolve and attach then/catch handlers.
Write code that iterates over the input, stores each result at its index, increments the counter on fulfillment, and resolves the outer promise when the counter equals the input length. Reject immediately on any rejection.
Walk through test cases: all resolve, one rejects, empty array, mixed values (promises and plain values), and order preservation. Ensure the implementation handles these correctly.
Talk about performance (O(n) time, O(n) space), error handling, and how your implementation compares to native Promise.all. Mention potential improvements like handling iterables or using async/await.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.