Skip to main content

Class: Context

activity.Context

Activity Context manager.

Call Context.current() from Activity code in order to send heartbeats and get notified of Activity cancellation.

Properties#

cancellationSignal#

Readonly cancellationSignal: AbortSignal

An AbortSignal which can be used to cancel requests on Activity cancellation.

Typically used by the fetch and child_process libraries but is supported by a few other libraries as well.


cancelled#

Readonly cancelled: Promise<never>

Await this promise in an Activity to get notified of cancellation.

This promise will never be resolved, it will only be rejected with a CancelledFailure.


info#

info: Info

Holds information about the current executing Activity.

Methods#

heartbeat#

heartbeat(details?): void

Send a heartbeat from an Activity.

If an Activity times out, the last value of details is included in the ActivityFailure delivered to a Workflow in the cause attribute which will be set to TimeoutFailure. Then the Workflow can pass the details to the next Activity invocation. This acts as a periodic checkpoint mechanism for the progress of an Activity.

The Activity must heartbeat in order to receive cancellation.

Parameters#

NameType
details?unknown

Returns#

void


sleep#

sleep(ms): Promise<void>

Helper function for sleeping in an Activity.

Parameters#

NameTypeDescription
msstring | numbersleep duration - ms formatted string or number of milliseconds

Returns#

Promise<void>

a Promise that either resolves when deadline is reached or rejects when the Context is cancelled


current#

Static current(): Context

Gets the context of the current Activity.

Uses AsyncLocalStorage under the hood to make it accessible in nested callbacks and promises.

Returns#

Context