|
| 1 | +# 2650. Design Cancellable Function |
| 2 | +[LeetCode](https://leetcode.com/problems/design-cancellable-function/) |
| 3 | + |
| 4 | +Sometimes you have a long running task, and you may wish to cancel it before it completes. To help with this goal, write a function `cancellable` that accepts a generator object and returns an array of two values: a cancel function and a promise. |
| 5 | +You may assume the generator function will only yield promises. It is your function's responsibility to pass the values resolved by the promise back to the generator. If the promise rejects, your function should throw that error back to the generator. |
| 6 | +If the cancel callback is called before the generator is done, your function should throw an error back to the generator. That error should be the string `"Cancelled"` (Not an `Error` object). If the error was caught, the returned promise should resolve with the next value that was yielded or returned. Otherwise, the promise should reject with the thrown error. No more code should be executed. |
| 7 | + |
| 8 | +When the generator is done, the promise your function returned should resolve the value the generator returned. If, however, the generator throws an error, the returned promise should reject with the error. |
| 9 | + |
| 10 | +An example of how your code would be used: |
| 11 | +```javascript |
| 12 | +function* tasks() { |
| 13 | + const val = yield new Promise(resolve => resolve(2 + 2)); |
| 14 | + yield new Promise(resolve => setTimeout(resolve, 100)); |
| 15 | + return val + 1; // calculation shouldn't be done. |
| 16 | +} |
| 17 | +const [cancel, promise] = cancellable(tasks()); |
| 18 | +setTimeout(cancel, 50); |
| 19 | +promise.catch(console.log); // logs "Cancelled" at t=50ms |
| 20 | +``` |
| 21 | +If instead `cancel()` was not called or was called after `t=100ms`, the promise would have resolved 5. |
| 22 | + |
| 23 | + |
| 24 | +## Example 1: |
| 25 | + |
| 26 | +Input: |
| 27 | +```javascript |
| 28 | +generatorFunction = function*() { |
| 29 | + return 42; |
| 30 | +} |
| 31 | +cancelledAt = 100 |
| 32 | +``` |
| 33 | +**Output:** `{"resolved": 42}`<br> |
| 34 | +**Explanation:** |
| 35 | +```javascript |
| 36 | +const generator = generatorFunction(); |
| 37 | +const [cancel, promise] = cancellable(generator); |
| 38 | +setTimeout(cancel, 100); |
| 39 | +promise.then(console.log); // resolves 42 at t=0ms |
| 40 | +``` |
| 41 | +The generator immediately yields 42 and finishes. Because of that, the returned promise immediately resolves 42. Note that cancelling a finished generator does nothing. |
| 42 | + |
| 43 | +## Example 2: |
| 44 | + |
| 45 | +Input: |
| 46 | +```javascript |
| 47 | +generatorFunction = function*() { |
| 48 | + const msg = yield new Promise(res => res("Hello")); |
| 49 | + throw `Error: ${msg}`; |
| 50 | +} |
| 51 | +cancelledAt = null |
| 52 | +``` |
| 53 | +**Output:** `{"rejected": "Error: Hello"}`<br> |
| 54 | +**Explanation:** |
| 55 | +A promise is yielded. The function handles this by waiting for it to resolve and then passes the resolved value back to the generator. Then an error is thrown which has the effect of causing the promise to reject with the same thrown error. |
| 56 | + |
| 57 | +## Example 3: |
| 58 | + |
| 59 | +Input: |
| 60 | +```javascript |
| 61 | +generatorFunction = function*() { |
| 62 | + yield new Promise(res => setTimeout(res, 200)); |
| 63 | + return "Success"; |
| 64 | +} |
| 65 | +cancelledAt = 100 |
| 66 | +``` |
| 67 | +**Output:** `{"rejected": "Cancelled"}`<br> |
| 68 | +**Explanation:** |
| 69 | +While the function is waiting for the yielded promise to resolve, cancel() is called. This causes an error message to be sent back to the generator. Since this error is uncaught, the returned promise rejected with this error. |
| 70 | + |
| 71 | +## Constraints: |
| 72 | + |
| 73 | +`cancelledAt == null or 0 <= cancelledAt <= 1000`<br> |
| 74 | +`generatorFunction` returns a generator object |
0 commit comments