1

I have seen questions like this but they do not answer my question.

I have a function

var kue = require('kue');
var jobs = kue.createQueue();

var create_job = function(data, callback){
    jobs.create('job', {
        stuff: data   
    }).save();
}

How can I make it so that callback is called when jobs.process('job', function(job, done){ ... is finished?

Community
  • 1
  • 1
Loourr
  • 4,995
  • 10
  • 44
  • 68

1 Answers1

3

I've not used kue before, so this is going off their documentation:

var kue = require('kue');
var jobs = kue.createQueue();

var create_job = function(data, callback){
    var job = jobs.create('job', {
        stuff: data   
    }).save();
    job.on('complete', function(){
        callback();
    }).on('failed', function(){
      callback({error: true});
    });    
};

I've created/used a JavaScript closure to capture the value of the callback argument (by attaching the events within the scope of the create_job function, and it's later called when the function completes (or fails).

WiredPrairie
  • 58,954
  • 17
  • 116
  • 143
  • Awesome this worked, thanks. This may be a misunderstanding of closures on my part, but will the `job` variable be garbage collected after one of the `.on()` is triggered? – Loourr Dec 29 '13 at 20:16
  • Yes, assuming that the `job` variable isn't referenced elsewhere. – WiredPrairie Dec 29 '13 at 20:45