What if your computer could remember and do your tasks for you, even when you're asleep?
Why Scheduled tasks (cron) in Deno? - Purpose & Use Cases
Imagine you need to send reminder emails every day at 9 AM, but you have to remember to run the script manually or keep your computer on all the time.
Manually running tasks is unreliable and easy to forget. Leaving your computer on wastes energy, and running scripts at the wrong time causes missed deadlines or duplicated work.
Scheduled tasks with cron let you automate running scripts at exact times without manual effort, making your programs run reliably and on schedule.
setTimeout(() => sendEmails(), 86400000); // runs once after 24 hours
import { Cron } from 'https://deno.land/x/cron/mod.ts'; const job = new Cron('0 9 * * *', () => sendEmails()); job.start();
Automating repetitive tasks to run on precise schedules frees you from manual work and ensures consistency.
A website automatically clears expired user sessions every night at midnight without anyone needing to start the process.
Manual task running is unreliable and inefficient.
Cron schedules tasks to run automatically at set times.
This saves time, reduces errors, and improves reliability.
Practice
Solution
Step 1: Understand scheduled tasks
Scheduled tasks run code automatically without manual intervention.Step 2: Identify cron job purpose in Deno
Deno's cron jobs automate running code at set times or intervals.Final Answer:
To run code automatically at specific times or intervals -> Option DQuick Check:
Scheduled tasks = automatic timed code run [OK]
- Confusing scheduled tasks with manual triggers
- Thinking cron is for debugging
- Assuming cron speeds up compilation
Solution
Step 1: Check Deno import style
Deno uses ES module imports with URLs, not CommonJS require.Step 2: Identify correct croner import
The official croner import uses a URL with version and named import.Final Answer:
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts"; -> Option CQuick Check:
Deno imports use URLs and named imports [OK]
- Using require() which is Node.js style
- Importing from 'cron' instead of 'croner'
- Using wildcard import without URL
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts";
const cron = new Cron("* * * * *", () => {
console.log("Hello every minute");
});
cron.start();
Solution
Step 1: Understand the cron pattern "* * * * *"
This pattern means the task runs every minute.Step 2: Analyze the callback behavior
The callback logs "Hello every minute" each time the cron triggers.Final Answer:
Prints "Hello every minute" every minute -> Option BQuick Check:
Cron pattern "* * * * *" = every minute [OK]
- Thinking it runs every second
- Expecting immediate output without waiting
- Confusing cron syntax with other formats
import { Cron } from "https://deno.land/x/croner@6.0.0/mod.ts";
const cron = new Cron("60 * * * *", () => {
console.log("This runs every hour");
});
cron.start();
Solution
Step 1: Check the cron pattern validity
Minutes field must be between 0 and 59; "60" is invalid.Step 2: Confirm other code parts
cron.start() does not require await; callback can be sync; hourly tasks are supported.Final Answer:
The cron pattern "60 * * * *" is invalid because minutes must be 0-59 -> Option AQuick Check:
Minutes range = 0-59, 60 is invalid [OK]
- Using invalid minute value 60
- Thinking cron.start() needs await
- Assuming callback must be async
Solution
Step 1: Understand cron fields order
Cron fields are: minute, hour, day of month, month, day of week.Step 2: Match 9:30 AM on weekdays
Minute = 30, hour = 9, day of week = 1-5 (Monday to Friday).Final Answer:
"30 9 * * 1-5" -> Option AQuick Check:
Minute=30, Hour=9, Weekdays=1-5 [OK]
- Swapping hour and minute fields
- Using weekend days 6,7 instead of weekdays
- Setting minute or hour to zero incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
