Explorer
Node.js

Write a function `withTimeout(promise, ms)` that rejects if the promise does not resolve within `ms` milliseconds.

Problem Statement

Write a function `withTimeout(promise, ms)` that rejects if the promise does not resolve within `ms` milliseconds.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Race the original promise against a timeout promise. šŸ’” Hint 2: Create a timeout promise that rejects after ms milliseconds. šŸ’” Hint 3: return Promise.race([promise, new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout")), ms))]); āœ… Solution: ```javascript function withTimeout(promise, ms) { const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout")), ms)); return Promise.race([promise, timeout]); } ```

Timeout Wrapper

Hard
Write a function `withTimeout(promise, ms)` that rejects if the promise does not resolve within `ms` milliseconds.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
"fast"