-
Notifications
You must be signed in to change notification settings - Fork 66
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
Delay between Jobs #81
Comments
Use |
Hi Jesse, could you provide an example code ? I’d like to use ˋqueueˋ to send e-mails and need to throttle the process to not exceed 4 sendings per seconds. Thx. |
@PlanetIrata I know this is a month old, but maybe it will be helpful to the next person. const q = queue({ concurrency: 1, autostart: true });
function sendThrottledEmail(email) {
q.push((cb) => {
_sendEmail(email);
setTimeout(cb, 250);
});
} You can also use a Promise. If const q = queue({ concurrency: 1, autostart: true });
function sendThrottledEmail(email) {
const delay = 1000 / 4;
q.push(
() =>
new Promise((resolve, reject) => {
_sendEmail(email)
.then(() => setTimeout(resolve, delay))
.catch((err) => setTimeout(() => reject(err), delay));
// or fail immediately and run the next job
// .catch(reject);
})
);
} |
Althugh the suggestions above may work - the right tool for your need will be a rate limiter / throttler, see this: https://www.npmjs.com/package/limiter |
How do I set a delay between jobs in a queue?
My jobs represent API calls
The text was updated successfully, but these errors were encountered: