- Functions
- Delays
Functions
Delays
You can trigger workflow steps to fire before and after a delay. There are 2 types of delay, waitFor and waitUntil.
waitFor
delay
waitFor
takes a unique key and an object that sets the desired time as a key value pair.
await ctx.waitFor("wait-for-1", { seconds: 30 });
Reminder: Ensure all keys are unique across all triggers.
Example
This example posts a message to Slack after a 30 second delay:
import { Trigger, customEvent } from "@trigger.dev/sdk";
import * as slack from "@trigger.dev/slack";
import * as github from "@trigger.dev/github";
import { z } from "zod";
new Trigger({
id: "wait-for-example",
name: "Post to Slack after delay",
apiKey: "<my_api_key>",
on: customEvent({
name: "waitForExample",
schema: z.object({
id: z.string(),
}),
}),
run: async (event, ctx) => {
await ctx.waitFor("initial-wait", { seconds: 30 });
const response = await slack.postMessage("send-to-slack", {
channel: "my-slack-channel",
text: `This message posted after a 30 second delay.`,
});
return response.message;
},
}).listen();
waitUntil
delay
waitUntil
takes a unique key and a date function to set desired time.
await ctx.waitUntil("wait-until-1", new Date(Date.now() + 1000 * 60 * 1));
Reminder: Ensure all keys are unique across all triggers.
Example
This example posts a message to Slack 1 hour after the workflow triggers:
import { Trigger, customEvent } from "@trigger.dev/sdk";
import * as slack from "@trigger.dev/slack";
import * as github from "@trigger.dev/github";
import { z } from "zod";
new Trigger({
id: "wait-until-example",
name: "Post to Slack after 1hr delay",
apiKey: "<my_api_key>",
on: customEvent({
name: "waitUntilExample",
schema: z.object({
id: z.string(),
}),
}),
run: async (event, ctx) => {
await ctx.waitUntil("wait-until", new Date(Date.now() + 1000 * 60 * 1));
const response = await slack.postMessage("send-to-slack", {
channel: "my-slack-channel",
text: `This message posted after a 30 second delay.`,
});
return response.message;
},
}).listen();