|
| 1 | +// Question Link: https://leetcode.com/problems/promise-time-limit/?envType=study-plan-v2&envId=30-days-of-javascript |
| 2 | +// Solution Link: |
| 3 | + |
| 4 | +/* |
| 5 | +2637. Promise Time Limit |
| 6 | +
|
| 7 | +Given an asynchronous function fn and a time t in milliseconds, return a new time limited version of the input function. fn takes arguments provided to the time limited function. |
| 8 | +
|
| 9 | +The time limited function should follow these rules: |
| 10 | +If the fn completes within the time limit of t milliseconds, the time limited function should resolve with the result. |
| 11 | +If the execution of the fn exceeds the time limit, the time limited function should reject with the string "Time Limit Exceeded". |
| 12 | + |
| 13 | +Example 1: |
| 14 | +Input: |
| 15 | +fn = async (n) => { |
| 16 | + await new Promise(res => setTimeout(res, 100)); |
| 17 | + return n * n; |
| 18 | +} |
| 19 | +inputs = [5] |
| 20 | +t = 50 |
| 21 | +Output: {"rejected":"Time Limit Exceeded","time":50} |
| 22 | +Explanation: |
| 23 | +const limited = timeLimit(fn, t) |
| 24 | +const start = performance.now() |
| 25 | +let result; |
| 26 | +try { |
| 27 | + const res = await limited(...inputs) |
| 28 | + result = {"resolved": res, "time": Math.floor(performance.now() - start)}; |
| 29 | +} catch (err) { |
| 30 | + result = {"rejected": err, "time": Math.floor(performance.now() - start)}; |
| 31 | +} |
| 32 | +console.log(result) // Output |
| 33 | +The provided function is set to resolve after 100ms. However, the time limit is set to 50ms. It rejects at t=50ms because the time limit was reached. |
| 34 | +
|
| 35 | +Example 2: |
| 36 | +Input: |
| 37 | +fn = async (n) => { |
| 38 | + await new Promise(res => setTimeout(res, 100)); |
| 39 | + return n * n; |
| 40 | +} |
| 41 | +inputs = [5] |
| 42 | +t = 150 |
| 43 | +Output: {"resolved":25,"time":100} |
| 44 | +Explanation: |
| 45 | +The function resolved 5 * 5 = 25 at t=100ms. The time limit is never reached. |
| 46 | +
|
| 47 | +Example 3: |
| 48 | +Input: |
| 49 | +fn = async (a, b) => { |
| 50 | + await new Promise(res => setTimeout(res, 120)); |
| 51 | + return a + b; |
| 52 | +} |
| 53 | +inputs = [5,10] |
| 54 | +t = 150 |
| 55 | +Output: {"resolved":15,"time":120} |
| 56 | +Explanation: |
| 57 | +The function resolved 5 + 10 = 15 at t=120ms. The time limit is never reached. |
| 58 | +
|
| 59 | +Example 4: |
| 60 | +Input: |
| 61 | +fn = async () => { |
| 62 | + throw "Error"; |
| 63 | +} |
| 64 | +inputs = [] |
| 65 | +t = 1000 |
| 66 | +Output: {"rejected":"Error","time":0} |
| 67 | +Explanation: |
| 68 | +The function immediately throws an error. |
| 69 | +
|
| 70 | +Constraints: |
| 71 | +0 <= inputs.length <= 10 |
| 72 | +0 <= t <= 1000 |
| 73 | +fn returns a promise |
| 74 | +*/ |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +/** |
| 79 | + * @param {Function} fn |
| 80 | + * @param {number} t |
| 81 | + * @return {Function} |
| 82 | + */ |
| 83 | + |
| 84 | +var timeLimit = function(fn, t) { |
| 85 | + |
| 86 | + return async function(...args) { |
| 87 | + const onSuccess = fn(...args); |
| 88 | + |
| 89 | + const timeoutPromise = new Promise((resolve, reject) => { |
| 90 | + setTimeout(() => reject("Time Limit Exceeded"), t); |
| 91 | + }); |
| 92 | + |
| 93 | + return Promise.race([onSuccess, timeoutPromise]); |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +/** |
| 98 | + * const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100); |
| 99 | + * limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms |
| 100 | + */ |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +# Code: |
| 114 | +``` |
| 115 | +/** |
| 116 | + * @param {Function} fn |
| 117 | + * @param {number} t |
| 118 | + * @return {Function} |
| 119 | + */ |
| 120 | + |
| 121 | +var timeLimit = function(fn, t) { |
| 122 | + |
| 123 | + return async function(...args) { |
| 124 | + const onSuccess = fn(...args); |
| 125 | +
|
| 126 | + const timeoutPromise = new Promise((resolve, reject) => { |
| 127 | + setTimeout(() => reject("Time Limit Exceeded"), t); |
| 128 | + }); |
| 129 | +
|
| 130 | + return Promise.race([onSuccess, timeoutPromise]); |
| 131 | + } |
| 132 | +}; |
| 133 | +
|
| 134 | +/** |
| 135 | + * const limited = timeLimit((t) => new Promise(res => setTimeout(res, t)), 100); |
| 136 | + * limited(150).catch(console.log) // "Time Limit Exceeded" at t=100ms |
| 137 | + */ |
| 138 | +``` |
| 139 | + |
| 140 | +<br/> |
| 141 | + |
| 142 | + |
0 commit comments