-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
base: async-utils
Are you sure you want to change the base?
Add async utils: callbackify, promisify, debounce, throttle #276
Conversation
return () => { | ||
if (timer) clearTimeout(timer); | ||
timer = setTimeout(debounced, timeout); | ||
timer = setTimeout(() => { | ||
fn(...args); | ||
timer = null; | ||
}, delay); | ||
}; |
There was a problem hiding this comment.
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
asyncFn(...args) | ||
.then((res) => callback(null, res)) | ||
.catch((err) => callback(err)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer fn
if (typeof callback !== 'function' || callback.length !== 2) { | ||
throw new Error('Last argument should be a function with 2 parameters'); | ||
} |
There was a problem hiding this comment.
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
FYI: Typings for callbacks |
npm run test
,npm run lint
)npm run fix
)Fixes #268