How do you implement a timeout with Promise.race in JavaScript?
Race the operation against a timeout promise: Promise.race([fetch('/api'), new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 5000))]). If the timeout fires first, the race rejects.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Promise.all: Examples and Use Cases in JavaScript
It runs all promises in parallel and waits for all to fulfill. If any rejects, the whole thing rejects (fast fail). Results are in the same order as the input promises. Non-promise values are passed through.
Yes. Promise.all rejects as soon as any promise rejects (fast fail). The other promises continue running (they are not cancelled), but their results are ignored. Use Promise.allSettled if you want all results regardless of failures.
An empty array []. Promise.all with an empty array resolves immediately (there is nothing to wait for). This is useful when you dynamically build an array and it might be empty.
Still have questions?
Browse all our FAQs or reach out to our support team
