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
  • 61,948
  • 13
  • 68
  • 95
Devaraj
  • 25
  • 6

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,889
  • 1
  • 19
  • 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,753
  • 8
  • 41
  • 63