I am having trouble accessing a class' methods without some modifications to the class itself.
See the following demo code:
function myCallback() {
this.otherMethod();
}
class hiddenClass {
static hiddenFunction(callback) {
callback();
}
static otherMethod(){
console.log("success");
}
}
hiddenClass.hiddenFunction(myCallback);
In this simple example, I want to access this.otherMethod() in my callback. The obvious answer is to change callback()
to callback.bind(this)()
, however in this case, hiddenClass would be a library.
How can I call this other method?
For reference, I am trying to create cron jobs using node-cron. I want to destroy these jobs if a database check returns true, checking every cycle of the job (in the callback). The job has an internal method .destroy()
. I am aware I can save the cron job in a variable, then call variable.destroy()
, but is there a way of doing it in the callback (as these jobs are created in a for loop and I don't want to pinpoint which one to destroy from inside the callback)
cron.schedule(`5 * * * * *`, async function () {
schedule = await Schedule.findById(schedule._id);
if (!schedule) this.destroy() // Destroy the job if schedule is not found (this returns Window)
}
);