Tela SDK for JavaScript
    Preparing search index...

    Represents a workstation task with event-driven status tracking and result polling.

    Tasks are always asynchronous and require polling to retrieve results. The Task class provides an event system for monitoring task lifecycle and automatic output validation.

    Task status can be accessed via the status property and tracks the lifecycle:

    • createdrunningcompleted or failed

    Status is set to failed when:

    • API request fails
    • Validation fails
    • Polling reports failure status

    Status is set to completed only after:

    • API request succeeds AND
    • Output validation passes (if schema provided)

    poll - Emitted during polling with current status and output. Includes requestId from the polling request.

    success - Emitted when task completes successfully with the final result. Includes requestId from the final request.

    error - Emitted when task fails (only if error listeners are registered, otherwise throws)

    statusChange - Emitted when task status transitions to a new state

    const task = await workstation.createTask({ query: 'test' })

    // Track status changes
    task.on('statusChange', (status) => {
    console.log(`Status: ${status}`) // created → running → completed
    })

    // Monitor polling progress
    task.on('poll', (pollResult) => {
    console.log('Polling...', pollResult.requestId)
    })

    // Handle success
    task.on('success', (result) => {
    console.log('Result:', result)
    })

    // Handle errors
    task.on('error', (error) => {
    console.error('Failed:', error)
    })

    // Start polling without waiting
    task.poll()

    Type Parameters

    • TInput = unknown

      The input variables type

    • TOutput = unknown

      The output result type

    Hierarchy

    Index

    Constructors

    • Type Parameters

      • TInput = unknown
      • TOutput = unknown

      Parameters

      • client: BaseClient
      • variables: TInput
      • task: TaskDefinition
      • OptionalrequestId: string
      • OptionaloutputSchema:
            | Record<string, unknown>
            | ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>
      • skipResultValidation: boolean = false

      Returns Task<TInput, TOutput>

    Accessors

    • get id(): string

      Gets the unique task ID assigned by the server.

      Returns string

      The task ID.

    • get label(): string

      Gets the task label (alias for name).

      Returns string

      The task name.

    • get name(): string

      Gets the task name.

      Returns string

      The task name.

    • get requestId(): string | undefined

      Gets the request ID from the x-request-id header of the task creation request.

      This is the request ID from the initial POST /task request that created the task. Each API request has its own unique request ID.

      For polling operations, different request IDs are available:

      • task.requestId - ID from the task creation request
      • pollResult.requestId - ID from each polling request (in poll events)
      • result.requestId - ID from the final successful polling request (in success events)

      Returns string | undefined

      A promise that resolves to the request ID from the task creation request.

      const task = await workstation.createTask({ query: 'test' })
      const requestId = await task.requestId
      console.log('Request ID:', requestId)
    • get result(): Promise<TOutput>

      Gets the task result. Automatically starts polling if not already started.

      Returns Promise<TOutput>

      A promise that resolves to the validated task output.

      const task = await workstation.createTask({ query: 'test' })
      const result = await task.result
      console.log(result)
    • get status(): | "created"
      | "failed"
      | "running"
      | "validating"
      | "completed"
      | "cancelled"

      Gets the latest known status of the task.

      Status values and transitions:

      • createdrunningcompleted or failed

      Important: Status is set to completed only after successful validation. If validation fails, status will be failed even if the API request succeeded.

      Use the statusChange event to track status transitions in real-time.

      Returns "created" | "failed" | "running" | "validating" | "completed" | "cancelled"

      The current status of the task.

      const task = await workstation.createTask({ query: 'test' })
      console.log(task.status) // 'created'

      await task.result
      console.log(task.status) // 'completed' or 'failed'
    • get tags(): string[]

      Gets the task tags.

      Returns string[]

      The task tags.

    • get variables(): TInput

      Gets the input variables provided to this task.

      Returns TInput

      The variables object.

    Methods

    • Starts polling for the task result without waiting for the promise to resolve. This allows users to track task progress via events rather than awaiting a promise.

      The following events will be emitted during polling:

      • statusChange: Emitted when the task status changes (e.g., 'created' → 'running' → 'completed')
      • poll: Emitted on each polling attempt with the server response
      • success: Emitted when the task completes successfully with the final result
      • error: Emitted if the task fails

      Important: Events are only emitted while polling is active. You must call poll() or await task.result to start polling. Simply setting up event listeners without starting polling will not trigger any events.

      Returns void

      const task = await workstation.createTask({ query: 'test' })

      // Set up event listeners
      task.on('statusChange', (status) => {
      console.log('Status:', status)
      })

      task.on('success', (result) => {
      console.log('Completed:', result)
      })

      task.on('error', (error) => {
      console.error('Failed:', error)
      })

      // Start polling without waiting
      task.poll()