Skip to content

Triggering from your app

Wait for a run

triggerAndWait starts a run and returns its output. runs.poll waits for a run you already started, until it finishes or a condition you choose is true.

On this page

Trigger and wait#

triggerAndWait starts a run, waits for it to finish, and returns its output:

import { tasks, RunFailedError, WaitTimeoutError } from '@bokjoy/sdk'; try {  const output = await tasks.triggerAndWait(    'email.send',    { email: 'grace@example.com', template: 'welcome' },    { idempotencyKey: 'welcome-grace', timeoutMs: 60_000 },  );  console.log('sent:', output.sent);} catch (error) {  if (error instanceof RunFailedError) {    console.error(`run ${error.run.status}: ${error.run.error}`);  } else if (error instanceof WaitTimeoutError) {    // The run keeps going. Read it again later.    console.error('still running:', error.run?.id);  } else {    throw error;  }}
The runtriggerAndWait
CompletesReturns its output.
Fails or is cancelledThrows RunFailedError. The run is on error.run, with its status and error.
Is still going at the timeoutThrows WaitTimeoutError. The run keeps going.

It takes the trigger options and the wait options below. Unlike Trigger.dev's triggerAndWait, it works from any backend, not only inside a task.

Wait for a run you started#

runs.poll waits for a run and returns it once it has finished, whatever its status. It never throws for a failed run, so check the status yourself. Pass until to stop earlier:

import { runs, tasks } from '@bokjoy/sdk'; const handle = await tasks.trigger(  'user.onboarding',  { email: 'ada@example.com' },  { idempotencyKey: 'signup-ada' },); // Stop as soon as the workflow waits, or once it finishes.const run = await runs.poll(handle, {  until: (run) => run.isWaiting,  timeoutMs: 120_000,}); console.log(run.status, run.attempts.length);

poll never changes the run. A wait that times out leaves it running.

Wait options#

NodePythonDefault
timeoutMstimeout (seconds)None. Past it, WaitTimeoutError.
pollIntervalMspoll_interval (seconds)1 second
untiluntilStop early when it returns true. poll only.
streamstreamtrue: follow the run stream, as below.
signalAn AbortSignal that ends the wait.

The timeout is a deadline for the whole wait: every request, retry and pause stops at it. For triggerAndWait it starts before the trigger request, so it covers the trigger too.

Timeouts#

WaitTimeoutError carries the run's last known state on error.run, or null (Python: None) when no answer came in time.

If the trigger itself got no answer in time, the run may or may not have started. error.idempotencyKey (Python: idempotency_key) is the key the client sent. Trigger again with that key to reach the same run without starting a second one.

How a wait follows the run#

A wait opens the run stream and reads the run each time its status changes, so it returns as soon as the run finishes. It also reads the run every 10 seconds in case a change went unseen. If the stream is unavailable or drops, the wait reads the run every poll interval instead. Pass stream: false to always poll.

Read the result later#

A wait is optional. Save the run's ID when you trigger, and read the run whenever you need it with runs.retrieve. Results are kept: Bok Joy never deletes a run.