You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In above, isThrottled = false assignment is done inside setTimeout callback. However, only one callback is pushed into task queue and it isn't executed until stack is empty (for loop has to be completed). isThrottled is always true => setTimeout isn't called => one callback (that was registered for initial false isThrottled) => cb executed at end and outputs last value => output: 1, 9999999.
The text was updated successfully, but these errors were encountered:
void5253
changed the title
Throttle task incorrect solution. (Decorators and forwarding, call/apply)
. (Decorators and forwarding, call/apply)
Nov 13, 2023
void5253
changed the title
. (Decorators and forwarding, call/apply)
Solution for throttle decorator is incorrect. (Decorators and forwarding, call/apply)
Nov 13, 2023
Original code: https://javascript.info/call-apply-decorators#throttle-decorator
Here's a small code snippet to show where it doesn't work.
Expected Output
1, 249204, 452039, ... , 9999999 (These are random increasing numbers)
Output
1, 9999999
Why does it fail?
In above,
isThrottled = false
assignment is done insidesetTimeout
callback. However, only one callback is pushed into task queue and it isn't executed until stack is empty (for loop has to be completed).isThrottled
is alwaystrue
=>setTimeout
isn't called => one callback (that was registered for initial falseisThrottled
) => cb executed at end and outputs last value => output: 1, 9999999.Correct Solution: #2844
This PR giving an alternative solution.
The text was updated successfully, but these errors were encountered: