# Polling

When working with asynchronous operations, many APIs return immediately with a job ID or task reference, requiring you to poll for completion status. The `loopUntil` option in `lam.httpRequest` and `lam.httpRequests` allows you to automatically poll an endpoint until a specific condition is met.

The polling functionality is optional; just add a `loopUntil` object with a condition function to your HTTP request.

**Important:** Polling is not enabled by default. You must explicitly specify polling behavior when creating your workflow steps.

### `loopUntil` Properties

| Property       | Type                         | Required | Default | Description                                                                        |
| -------------- | ---------------------------- | -------- | ------- | ---------------------------------------------------------------------------------- |
| `condition`    | String (JavaScript function) | Yes      | -       | Function that receives `ctx` object and returns boolean. When true, polling stops. |
| `maxAttempts`  | Number                       | No       | 10      | Maximum number of polling attempts before giving up                                |
| `strategy`     | String                       | No       | 'fixed' | Delay strategy: 'fixed' or 'exponential'                                           |
| `initialDelay` | String                       | No       | '1s'    | Initial delay between polls (e.g., '1s', '500ms', '2m')                            |
| `maxDelay`     | String                       | No       | '30s'   | Maximum delay between polls for exponential strategy                               |
| `multiplier`   | Number                       | No       | 2       | Multiplier for exponential backoff strategy                                        |

The condition function receives a `ctx` object with:

* `ctx.response` - The parsed response body
* `ctx.status` - HTTP status code
* `ctx.headers` - Response headers

**Example:** Suppose we're starting a data processing job that takes time to complete, and we need to wait for it to finish before proceeding.

**Start Data Processing Job**

```javascript
(data) => {
  const { input } = data;

  return {
    "lam.httpRequest": {
      method: "POST",
      url: "{{config.dataProcessorUrl}}/api/jobs",
      headers: {
        Authorization: "Bearer {{config.processorApiKey}}",
        "Content-Type": "application/json",
      },
      body: {
        datasetId: input.datasetId,
        operation: "transform",
        outputFormat: "parquet",
      },
      // Poll until job completes
      loopUntil: {
        condition: "(ctx) => ctx.response.status === 'completed'",
        maxAttempts: 20,
        strategy: "exponential",
        initialDelay: "2s",
        maxDelay: "60s",
      },
    },
  };
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.laminar.run/advanced/polling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
