-
Notifications
You must be signed in to change notification settings - Fork 56
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
Support put job with loop #3
Comments
You're creating three clients there, once per trip through the loop, which is probably not what you want. If you're looping through data that you want to do something asynchronous with, use a continuer function. I updated the emitjobs.js example to show this off, but here's a stripped down example: var fivebeans = require('fivebeans');
var joblist = [ 'one', 'two', 'three', 'four', 'five' ];
var doneEmittingJobs = function()
{
console.log('We reached our completion callback. Now closing down.');
emitter.end();
process.exit(0);
};
var continuer = function(err, jobid)
{
console.log('emitted job id: ' + jobid);
if (joblist.length === 0)
return doneEmittingJobs();
emitter.put(0, 0, 60, JSON.stringify(['testtube', joblist.shift()]), continuer);
};
var emitter = new fivebeans.client('localhost', 11300);
emitter.connect(function(err)
{
emitter.use('testtube', function(err, tname)
{
console.log("using " + tname);
emitter.put(0, 0, 60, JSON.stringify(['testtube', joblist.shift()]), continuer);
});
}); First you define a continuer function that knows how to tell when you've looped through all your data and knows how to kick off processing on the next chunk of data. Then you invoke your processing on the first data item & pass the continuer as a callback. This is a general pattern you'll use often when looping through data in node. There's a pretty good explanation of this pattern in this article, with a more for-loop-y variation on it. Or you could use a flow control module to help with loops. I often use async for this. |
@ceejbot Thank you provide those information:) |
hi mate,
I try to put jobs with loop as below, but it seem need to exit process.
Is there any better idea?
thanks you.
The text was updated successfully, but these errors were encountered: