Working with Golem Promises in TypeScript
Golem promises provide a way for Golem workers to wait on an external condition. The worker creates the promise and somehow sends its identifier to the external system responsible for completing the promise. Then the worker can await the promise, being suspended until the external system completes the promise using Golem's REST API.
It is also possible to complete a promise from within a Golem worker using the Golem SDK.
When a promise is completed, an arbitrary byte array can be attached to it as a payload - this data is returned to the awaiting worker when is continues execution.
In TypeScript the promise API has idiomatic TypeScript wrappers in the Golem TypeScript SDK.
Creating a promise
To create a promise simply call the createPromise
function:
import { createPromise, PromiseId } from "@golemcloud/golem-ts"
const promiseId: PromiseId = createPromise()
The returned value has the type PromiseId
, and defined as the following (including the nested types):
export type OplogIndex = bigint
export interface Uuid {
highBits: bigint
lowBits: bigint
}
// Represents a Component
export interface ComponentId {
uuid: Uuid
}
// Represents a Worker
export interface WorkerId {
componentId: ComponentId
workerName: string
}
// A promise ID is a value that can be passed to an external Golem API to complete that promise
// from an arbitrary external source, while Golem workers can await for this completion.
export interface PromiseId {
workerId: WorkerId
oplogIdx: OplogIndex
}
Deleting a promise
If a promise is no longer used, it has to be deleted with:
import { deletePromise, PromiseId } from "@golemcloud/golem-ts"
deletePromise(promiseId)
Awaiting a promise
To await a promise, use the awaitPromise
functions, which returns the promise result as a byte array payload.
Here's an example that awaits a promise, then decodes the payload from JSON format:
import { awaitPromise, PromiseId } from "@golemcloud/golem-ts"
const byteArrayPayload: Uint8Array = awaitPromise(promiseId)
const payload = JSON.parse(new TextDecoder().decode(byteArrayPayload))
Completing a promise from within a worker
To complete a promise from within a worker, use the completePromise
function.
The following example completes a promise with a value encoded as JSON:
import { completePromise, PromiseId } from "@golemcloud/golem-ts"
const payload = {
id: "value",
meta: "data",
}
const byteArrayPayload: Uint8Array = new TextEncoder().encode(JSON.stringify(payload))
const success: boolean = completePromise(promiseId, byteArrayPayload)
Completing a promise from an external source
To see how to use the promise ID to complete a promise through the external REST API, check the REST API documentation.