Implement a function promiseTimeout(promise, ms) that either resolves with the value of the given promise if it finishes within the specified time or rejects with an error if the timeout duration is exceeded.
The function should:
"Timeout" if the timeout is reached first.Examples
const slowPromise = new Promise(res => setTimeout(() => res("done"), 2000)); promiseTimeout(slowPromise, 1000).catch(console.error); // "Timeout" const fastPromise = new Promise(res => setTimeout(() => res("done"), 500)); promiseTimeout(fastPromise, 1000).then(console.log); // "done"
JavaScript Function
No test results yet
Click "Run" to execute tests