# 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",
      },
    },
  };
};
```
