-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -46,69 +46,56 @@ const timeoutify = (promise, msec) => | |||||||||||
); | ||||||||||||
}); | ||||||||||||
|
||||||||||||
const throttle = (timeout, fn, ...args) => { | ||||||||||||
let timer; | ||||||||||||
let wait = false; | ||||||||||||
|
||||||||||||
const execute = args | ||||||||||||
? (...pars) => (pars ? fn(...args, ...pars) : fn(...args)) | ||||||||||||
: (...pars) => (pars ? fn(...pars) : fn()); | ||||||||||||
|
||||||||||||
const delayed = (...pars) => { | ||||||||||||
timer = undefined; | ||||||||||||
if (wait) execute(...pars); | ||||||||||||
}; | ||||||||||||
|
||||||||||||
const throttled = (...pars) => { | ||||||||||||
if (!timer) { | ||||||||||||
timer = setTimeout(delayed, timeout, ...pars); | ||||||||||||
wait = false; | ||||||||||||
execute(...pars); | ||||||||||||
const throttle = (fn, interval, ...presetArgs) => { | ||||||||||||
if (typeof interval !== 'number' || interval < 0) { | ||||||||||||
throw new Error('Interval must be greater then 0'); | ||||||||||||
} | ||||||||||||
let lastTime = 0; | ||||||||||||
return (...args) => { | ||||||||||||
const now = Date.now(); | ||||||||||||
if (now - lastTime < interval) return; | ||||||||||||
try { | ||||||||||||
fn(...presetArgs, ...args); | ||||||||||||
} finally { | ||||||||||||
lastTime = now; | ||||||||||||
} | ||||||||||||
wait = true; | ||||||||||||
}; | ||||||||||||
|
||||||||||||
return throttled; | ||||||||||||
}; | ||||||||||||
|
||||||||||||
const debounce = (timeout, fn, ...args) => { | ||||||||||||
let timer; | ||||||||||||
|
||||||||||||
const debounced = () => (args ? fn(...args) : fn()); | ||||||||||||
|
||||||||||||
const wrapped = () => { | ||||||||||||
const debounce = (fn, delay, ...args) => { | ||||||||||||
let timer = null; | ||||||||||||
return () => { | ||||||||||||
if (timer) clearTimeout(timer); | ||||||||||||
timer = setTimeout(debounced, timeout); | ||||||||||||
timer = setTimeout(() => { | ||||||||||||
fn(...args); | ||||||||||||
timer = null; | ||||||||||||
}, delay); | ||||||||||||
}; | ||||||||||||
|
||||||||||||
return wrapped; | ||||||||||||
}; | ||||||||||||
|
||||||||||||
const callbackify = | ||||||||||||
(fn) => | ||||||||||||
(asyncFn) => | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer |
||||||||||||
(...args) => { | ||||||||||||
const callback = args.pop(); | ||||||||||||
fn(...args) | ||||||||||||
.then((value) => { | ||||||||||||
callback(null, value); | ||||||||||||
}) | ||||||||||||
.catch((reason) => { | ||||||||||||
callback(reason); | ||||||||||||
}); | ||||||||||||
if (typeof callback !== 'function' || callback.length !== 2) { | ||||||||||||
throw new Error('Last argument should be a function with 2 parameters'); | ||||||||||||
} | ||||||||||||
Comment on lines
+80
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||
asyncFn(...args) | ||||||||||||
.then((res) => callback(null, res)) | ||||||||||||
.catch((err) => callback(err)); | ||||||||||||
Comment on lines
+83
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
}; | ||||||||||||
|
||||||||||||
const asyncify = | ||||||||||||
(fn) => | ||||||||||||
(...args) => { | ||||||||||||
const callback = args.pop(); | ||||||||||||
setTimeout(() => { | ||||||||||||
let result; | ||||||||||||
try { | ||||||||||||
result = fn(...args); | ||||||||||||
const result = fn(...args); | ||||||||||||
callback(null, result); | ||||||||||||
} catch (error) { | ||||||||||||
return void callback(error); | ||||||||||||
callback(error); | ||||||||||||
} | ||||||||||||
callback(null, result); | ||||||||||||
}, 0); | ||||||||||||
}; | ||||||||||||
|
||||||||||||
|
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