|
12 | 12 | <p><strong class="example">Example 1:</strong></p>
|
13 | 13 |
|
14 | 14 | <pre>
|
15 |
| -<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20, cancelTime = 50 |
| 15 | +<strong>Input:</strong> fn = (x) => x * 5, args = [2], t = 20 |
16 | 16 | <strong>Output:</strong> [{"time": 20, "returned": 10}]
|
17 | 17 | <strong>Explanation:</strong>
|
18 |
| -const cancel = cancellable(fn, [2], 20); // fn(2) called at t=20ms |
19 |
| -setTimeout(cancel, 50); |
| 18 | +const cancelTime = 50 |
| 19 | +const cancel = cancellable((x) => x * 5, [2], 20); // fn(2) called at t=20ms |
| 20 | +setTimeout(cancel, cancelTime); |
20 | 21 |
|
21 |
| -the cancelTime (50ms) is after the delay time (20ms), so fn(2) should be called at t=20ms. The value returned from fn is 10. |
| 22 | +The cancellation was scheduled to occur after a delay of cancelTime (50ms), which happened after the execution of fn(2) at 20ms. |
22 | 23 | </pre>
|
23 | 24 |
|
24 | 25 | <p><strong class="example">Example 2:</strong></p>
|
25 | 26 |
|
26 | 27 | <pre>
|
27 |
| -<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100, cancelTime = 50 |
| 28 | +<strong>Input:</strong> fn = (x) => x**2, args = [2], t = 100 |
28 | 29 | <strong>Output:</strong> []
|
29 |
| -<strong>Explanation:</strong> fn(2) was never called because cancelTime (50ms) is before the delay time (100ms). |
| 30 | +<strong>Explanation:</strong> |
| 31 | +const cancelTime = 50 |
| 32 | +const cancel = cancellable((x) => x**2, [2], 100); // fn(2) not called |
| 33 | +setTimeout(cancel, cancelTime); |
| 34 | + |
| 35 | +The cancellation was scheduled to occur after a delay of cancelTime (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called. |
30 | 36 | </pre>
|
31 | 37 |
|
32 | 38 | <p><strong class="example">Example 3:</strong></p>
|
33 | 39 |
|
34 | 40 | <pre>
|
35 |
| -<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelTime = 100 |
| 41 | +<strong>Input:</strong> fn = (x1, x2) => x1 * x2, args = [2,4], t = 30 |
36 | 42 | <strong>Output:</strong> [{"time": 30, "returned": 8}]
|
37 |
| -<strong>Explanation:</strong> fn(2, 4) was called at t=30ms because cancelTime > t. |
| 43 | +<strong>Explanation:</strong> |
| 44 | +const cancelTime = 100 |
| 45 | +const cancel = cancellable((x1, x2) => x1 * x2, [2,4], 30); // fn(2,4) called at t=30ms |
| 46 | +setTimeout(cancel, cancelTime); |
| 47 | + |
| 48 | +The cancellation was scheduled to occur after a delay of cancelTime (100ms), which happened after the execution of fn(2,4) at 30ms. |
38 | 49 | </pre>
|
39 | 50 |
|
40 | 51 | <p> </p>
|
|
0 commit comments