Sleep
Pause a task for minutes, hours, days, or weeks
The sleep API takes in a duration as a millisecond formatted
string and returns a promise that resolves when the sleep finishes.
These are examples of valid sleep durations:
Syntax | Description |
---|---|
"3000ms" | 3000 milliseconds |
"30s" | 30 seconds |
"10m" | 10 minutes |
"1h" | 1 hour |
"2d" | 2 days |
The sleep duration cannot exceed the length of the task's timeout. Tasks with the standard runtime
have a maximum exeuction time of 12 hours, while tasks with the workflow runtime can run up to 60
days. Tasks with the standard runtime will actively run until the promise resolves, but the
workflow runtime can stop running entirely and continue when the sleep ends. See
runtimes to choose which runtime is best suited for your use case.
Examples
Drip email campaign
With the sleep API and workflow runtime, you can create a drip email campaign.
The following example sends an email immediately and then waits a week before sending the second
email in the drip campaign.
typescriptCopied1import airplane from "airplane";23export default airplane.task(4{5slug: "email_campaign",6parameters: {7email: { name: "email", slug: "email", type: "shorttext" },8name: { name: "name", slug: "name", type: "shorttext" },9},10runtime: "workflow",11},12async (params) => {13await send_email({14email: params.email,15name: params.name,16subject: "Welcome to Airplane!",17message: `Hello, ${params.name}! To get started with Airplane, visit the docs: https://docs.airplane.dev`,18});1920// Sleep for 7 days before sending the next email21await airplane.sleep("7d");2223await send_email({24email: params.email,25name: params.name,26subject: "Getting started with Airplane",27message: `Hello, ${params.name}! Check out our [templates gallery](https://docs.airplane.dev/templates) for common use cases. As you give things a try, let us know if you have any feedback.`,28});29}30);3132export const send_email = async (params: {33email: string;34name: string;35subject: string;36message: string;37}): Promise<number> => {38const run = await airplane.email.message(39"my_email",40{ email: "example@airplane.dev", name: "Airplane" },41[{ email: params.email, name: params.name }],42{43subject: params.subject,44message: params.message,45}46);47return run.output.number_of_recipients;48};
javascriptCopied1import airplane from "airplane";23export default airplane.task(4{5slug: "email_campaign",6parameters: {7email: { name: "email", slug: "email", type: "shorttext" },8name: { name: "name", slug: "name", type: "shorttext" },9},10runtime: "workflow",11},12async (params) => {13await send_email({14email: params.email,15name: params.name,16subject: "Welcome to Airplane!",17message: `Hello, ${params.name}! To get started with Airplane, visit the docs: https://docs.airplane.dev`,18});1920// Sleep for 7 days before sending the next email21await airplane.sleep("7d");2223await send_email({24email: params.email,25name: params.name,26subject: "Getting started with Airplane",27message: `Hello, ${params.name}! Check out our [templates gallery](https://docs.airplane.dev/templates) for common use cases. As you give things a try, let us know if you have any feedback.`,28});29}30);3132export const send_email = async (params: {33email: string,34name: string,35subject: string,36message: string,37}) => {38const run = await airplane.email.message(39"my_email",40{ email: "example@airplane.dev", name: "Airplane" },41[{ email: params.email, name: params.name }],42{43subject: params.subject,44message: params.message,45}46);47return run.output.number_of_recipients;48};
Custom timeouts
You can use
sleep
to apply timeouts to operations, such as executing tasks or prompting operators.
This example creates a background prompt that waits for an
operator to respond. Normally, this task would wait for the prompt until the run times out, but the
sleep API can be used to enforce a shorter timeout.typescriptCopied1import airplane from "airplane";23export default airplane.task(4{5slug: "custom_timeout",6},7async () => {8// ...910const prompt = await airplane.prompt.background({11ok: {12type: "boolean",13description: "Are you sure you want to continue?",14},15});1617// Wait for the prompt to be submitted, or for an hour to pass.18await Promise.race([airplane.sleep("1h"), prompt.wait()]);1920if (!prompt.ok) {21console.log("Prompt not approved.");22return;23}2425const run = await airplane.execute("my_task");26return run.output;27}28);
javascriptCopied1import airplane from "airplane";23export default airplane.task(4{5slug: "custom_timeout",6},7async () => {8// ...910const prompt = await airplane.prompt.background({11ok: {12type: "boolean",13description: "Are you sure you want to continue?",14},15});1617// Wait for the prompt to be submitted, or for an hour to pass.18await Promise.race([airplane.sleep("1h"), prompt.wait()]);1920if (!prompt.ok) {21console.log("Prompt not approved.");22return;23}2425const run = await airplane.execute("my_task");26return run.output;27}28);