ololo
ololo

Reputation: 2076

How can I pass additional data to a scheduled job function in node-schedule

I have the classic scenario that has already been done before by other systems.

In my use case, users submit a date and an email message to be sent at that date. I'm using the node-schedule library to schedule these email deliveries. Here's the part of my code that handles scheduling:

async function scheduleEmail(date, content) {
    const sendDate = new Date(date);
    const job = schedule.scheduleJob(sendDate, async function () {
        // TODO: Send the email here
        console.log('Sending email:', content);
    });
}

I'm a bit confused about how the scheduleJob function will know which content to send when the scheduled time is reached. The function only takes a date parameter, so how will it retrieve and pass the corresponding email content back to me for sending?

Can anyone please clarify how this part of the code should work?

Upvotes: 0

Views: 101

Answers (1)

You are passing content variable to your job function. This variable will be saved even function is stopped.

Upvotes: 0

Related Questions