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

Add async utils: callbackify, promisify, debounce, throttle #276

Open
wants to merge 2 commits into
base: async-utils
Choose a base branch
from

Conversation

ArtemDmitrenko
Copy link

@ArtemDmitrenko ArtemDmitrenko commented Jan 4, 2025

  • tests and linter show no problems (npm run test, npm run lint)
  • tests are added/updated for bug fixes and new features
  • code is properly formatted (npm run fix)
  • description of changes is added in CHANGELOG.md
  • update .d.ts typings

Fixes #268

Comment on lines +67 to 73
return () => {
if (timer) clearTimeout(timer);
timer = setTimeout(debounced, timeout);
timer = setTimeout(() => {
fn(...args);
timer = null;
}, delay);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use named functions as in prev implementation, it is much more readable than unnamed arrows

Comment on lines +83 to +85
asyncFn(...args)
.then((res) => callback(null, res))
.catch((err) => callback(err));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
asyncFn(...args)
.then((res) => callback(null, res))
.catch((err) => callback(err));
fn(...args)
.then((res) => void callback(null, res), (err) => void callback(err));

};

const callbackify =
(fn) =>
(asyncFn) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer fn

Comment on lines +80 to +82
if (typeof callback !== 'function' || callback.length !== 2) {
throw new Error('Last argument should be a function with 2 parameters');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need such a check, callback may handle just first argument error and will be thrown while it is ok

@tshemsedinov
Copy link
Member

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 this pull request may close these issues.

2 participants