Skip to content
VaelJS Blog

vael:cron

UTC five-field cron scheduler and next-occurrence calculator.

Available in the vjs (vaeljs) runtime only; absent in vjs-lite. Evaluates standard five-field UTC expressions (minute hour dom month dow) with overlap and missed-tick protection.

import ... from "vael:cron"
Since v0.0.0-alpha.0 vjs (V8)
Cargo feature: Always enabled in vaeljs-core (absent in vaeljs-lite-core)
import { cron, next } from "vael:cron";
const upcoming = next("0 9 * * 1-5", new Date("2026-01-01T00:00:00Z"));
console.log(upcoming?.toISOString());
const job = cron("* * * * *", () => {
console.log("tick");
});
job.dispose();
functionSince v0.0.0-alpha.0vjs only
export function cron(
  expression: string,
  callback: () => void | PromiseLike<unknown>,
  options?: CronOptions,
): CronJob;

Schedule callback on a UTC five-field cron expression (minute hour day-of-month month day-of-week, max 512 bytes).

At most 1024 active jobs may exist concurrently. One callback executes at a time; returned Promises are awaited before the next tick is eligible.

Parameters
NameTypeDescription
expressionstringFive-field UTC cron expression (no extended L/W/#/?).
callback() => void | PromiseLike<unknown>Invoked with no arguments on each eligible tick.
options optionalCronOptionsOptional { onEvent } observer.
Returns
CronJob — Frozen CronJob handle whose dispose() stops future ticks.
Exceptions
  • TypeError — on invalid arguments or unknown options.
  • CronError — with code "VAEL_CRON_EXPRESSION", "VAEL_CRON_LIMIT", "VAEL_CRON_NO_OCCURRENCE", or "VAEL_CRON_ARGUMENT".
import { cron } from "vael:cron";
const job = cron("0 * * * *", async () => {
console.log("hourly job");
}, {
onEvent: (ev) => console.warn(ev.type),
});
job.dispose();
functionSince v0.0.0-alpha.0vjs only
export function next(expression: string, after?: Date | number): Date | null;

Compute the first UTC occurrence strictly after after, or null if none exists within the 100-year (366 * 100 days) search span or Date range.

Parameters
NameTypeDescription
expressionstringFive-field UTC cron expression (max 512 bytes).
after optionalDate | numberReference Date or epoch milliseconds (defaults to current host UTC time).
Returns
Date | null — Next matching UTC Date, or null when no occurrence exists in range.
Exceptions
  • TypeError — when expression or after has an invalid type.
  • RangeError — when after is an invalid Date or non-finite number.
  • CronError — with code "VAEL_CRON_EXPRESSION" or "VAEL_CRON_ARGUMENT".
import { next } from "vael:cron";
const after = new Date("2026-01-01T00:00:00Z");
console.log(next("30 12 * * *", after)?.toISOString());
classSince v0.0.0-alpha.0vjs only
export class CronError extends Error {
  override name: "CronError";
  code: CronErrorCode;
  constructor(code: CronErrorCode, message: string);
}

Error thrown for invalid cron expressions, job limits, missing future occurrences, or invalid cron arguments.

Constructor
constructor(code: CronErrorCode, message: string);

Construct a CronError.

Parameters
NameTypeDescription
codeCronErrorCodeMachine-readable CronErrorCode.
messagestringHuman-readable error message.
Properties
PropertyTypeDescriptionSince
name "CronError"Error name ("CronError").v0.0.0-alpha.0
code CronErrorCodeMachine-readable cron error code.v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs only
export interface CronOptions {
  onEvent?: (event: CronEvent) => void;
}

Options accepted by cron(expression, callback, options).

Properties
PropertyTypeDescriptionSince
onEvent optional(event: CronEvent) => voidObserver invoked with frozen overlap, missed, or error events. Errors thrown by onEvent itself are discarded so the schedule stays alive.v0.0.0-alpha.0
interfaceSince v0.0.0-alpha.0vjs only
export interface CronJob {
  dispose(): void;
}

Frozen handle controlling a scheduled UTC cron job.

Undisposed jobs keep the event loop alive. [Symbol.dispose] is also installed as an alias for dispose when Symbol.dispose exists in the engine.

Methods
methodSince v0.0.0-alpha.0
dispose(): void;

Cancel any pending timer wait and release the host job resource. Idempotent; does not interrupt a callback that is already running.

Returns
void
typeSince v0.0.0-alpha.0vjs only
export type CronErrorCode =
  | "VAEL_CRON_EXPRESSION"
  | "VAEL_CRON_LIMIT"
  | "VAEL_CRON_NO_OCCURRENCE"
  | "VAEL_CRON_ARGUMENT";

Error codes reported on CronError.code.

typeSince v0.0.0-alpha.0vjs only
export type CronEvent =
  | {
      /** Tick arrived while the previous callback was still running and was skipped. @since 0.0.0-alpha.0 */
      readonly type: "overlap";
      /** Scheduled UTC occurrence time. @since 0.0.0-alpha.0 */
      readonly scheduledTime: Date;
      /** Actual UTC wake-up time. @since 0.0.0-alpha.0 */
      readonly wokeAt: Date;
    }
  | {
      /** Wake-up was late enough that the following occurrence was already due; stale tick was skipped. @since 0.0.0-alpha.0 */
      readonly type: "missed";
      /** Scheduled UTC occurrence time that was missed. @since 0.0.0-alpha.0 */
      readonly scheduledTime: Date;
      /** Actual UTC wake-up time. @since 0.0.0-alpha.0 */
      readonly wokeAt: Date;
    }
  | {
      /** Callback threw synchronously, returned a rejected Promise, or the scheduler encountered a wait error. @since 0.0.0-alpha.0 */
      readonly type: "error";
      /** Thrown or rejected value. @since 0.0.0-alpha.0 */
      readonly error: unknown;
      /** `true` if a fatal scheduler error terminated the job; `false` for a callback error. @since 0.0.0-alpha.0 */
      readonly fatal: boolean;
    };

Scheduler lifecycle event passed to options.onEvent.