β³
Promise with Timeout
Promise with Timeoutλ³΄ν΅ λΉλκΈ° +20pts
Problem
Write a function that wraps a promise with a timeout.
Examples
Input:
await withTimeout(slowPromise, 1000)Output:
νμμμ μ μλ¬Explanation
μ΄ λ¬Έμ λ **Promise.race**λ₯Ό νμ©νμ¬ νμμμμ ꡬννλ λ°©λ²μ νμ΅ν©λλ€. ## ν΅μ¬ κ°λ : Promise νμμμ μ§μ λ μκ° λ΄μ μλ£λμ§ μμΌλ©΄ μ€ν¨ μ²λ¦¬ν©λλ€. ### ꡬν ```javascript function withTimeout(promise, ms) { const timeout = new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), ms) ); return Promise.race([promise, ...
View detailed explanation βKey Concepts
Promise νμμμ Promise.race setTimeout μλ¬ μ²λ¦¬
Time: O(1) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.