Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arguments get reset if you call debounced function within itself #26

Closed
archive64 opened this issue Feb 21, 2019 · 1 comment · Fixed by #32
Closed

arguments get reset if you call debounced function within itself #26

archive64 opened this issue Feb 21, 2019 · 1 comment · Fixed by #32

Comments

@archive64
Copy link

Summary

👋 Hi there! I've noticed that calling a debounced function from itself sets its arguments to undefined.

Minimal working example

debounce = require('debounce');

const func = debounce((x) => {
  console.log(x);
  func(2);
}, 1000);

func(1);

Actual output

1
undefined
undefined
undefined
// ...

Expected output

1
2
2
2
// ...

Workaround

As a temporary workaround, I can defer the call:

  setTimeout(() => func(2));

Cause

https://github.com/component/debounce/blob/master/index.js#L27-L28

result = func.apply(context, args); // calls debounced function, which sets args
context = args = null; // args gets reset here
@aaronbeall
Copy link
Contributor

I just ran into (as the programming gods would have it, a couple hours chasing red herrings until I finally found this issue). You can reproduce this on NPM RunKit:

const debounce = require("debounce")

const fn = debounce(n => {
  console.log("fn", n)
  --n && fn(n);
});

fn(3)

Expect:

3
2
1

Actual:

3
undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants