0

I'm new to nodejs and i need to know how to pass the parameter to callback function.

function scheduler(key, cron, callback){

  //cron-job-manager
  manager.add('key', '* 30 * * * *', callback)
}

function callback(key,cron){
 console.log(cron);
}

schdeduler("key", " * * * * *", callback);

Thanks in advance.

Andy
  • 53,323
  • 11
  • 64
  • 89
Devaraj
  • 25
  • 5

2 Answers2

1

You can use a closure for your callback. You need to move callback function inside scheduler:

function scheduler(key, cron, callback){

  function callback() {
    console.log(key);
    console.log(cron);
  }

  //cron-job-manager
  manager.add(key, cron, callback)
}

schdeduler("key", " * * * * *", callback);

OR use bind:

function scheduler(key, cron, callback){

  //cron-job-manager
  manager.add(key, cron, callback.bind(this, key, cron))
}

function callback(key, cron) {
  console.log(key);
  console.log(cron);
}

schdeduler("key", " * * * * *", callback);
phts
  • 3,819
  • 1
  • 17
  • 31
0

as per the comment in your cese it would be:

function callback(key, cron) {
    console.log(key + ", " + cron);
}

function scheduler(key, cron, callback) {
    //manager.add('key', '* 30 * * * *', callback);
    callback();
}

scheduler("key", " * * * * *", function() {
    callback("key", " * * * * *");
});
vidriduch
  • 4,643
  • 7
  • 42
  • 60