> ## Documentation Index
> Fetch the complete documentation index at: https://docs.goldsky.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Task to Task Execution (callTask)

## Cross task execution

A common pattern in compose apps is to trigger tasks from other tasks. This can be done either by creating an [HTTP trigger](../task-triggers)
and calling it with [fetch()](./fetch), or by using `callTask()`. Typically you'll want to use `callTask()`.

`callTask` invokes another task **in the same compose app**. It awaits the invoked task's `main()` function and resolves with its return value. If the invoked task throws, the error is re-thrown in the caller.

```typescript theme={null}
callTask<Args = Record<string, unknown>, T = unknown>(
  taskName: string,
  args: Args,
  retryConfig?: {
    max_attempts: number;
    initial_interval_ms: number;
    backoff_factor: number;
  },
): Promise<T>
```

* `taskName` — the `name` of a task declared in `compose.yaml`.
* `args` — a JSON-serializable payload passed to the invoked task's `main()` as its payload argument.
* `retryConfig` — optional. Overrides the invoked task's retry settings for this call.

### Basic Example

```typescript theme={null}
import { TaskContext } from "compose";

export async function main({ callTask }: TaskContext) {
  const result = await callTask("my_task", { some: "payload" });
  return result;
}
```

### Examples

#### Call task in a loop

```typescript highlight={18-28} theme={null}
import { TaskContext } from "compose";

type MarketData = {
  questionId: string;
  resolved: boolean;
  startTime: number;
  endTime: number;
};

export async function main({ collection, callTask }: TaskContext) {
  const marketsCollection = await collection<MarketData>("markets");
  const unresolvedMarkets = await marketsCollection.findMany({ resolved: false });

  const SYMBOL = "ETH-USD";

  const marketDataPromises = unresolvedMarkets.map(async (market) => {
    try {
      const response = await callTask<{ error?: string }>("readMarketData", {
        questionId: market.questionId,
        symbol: SYMBOL,
        startTime: market.startTime,
        endTime: market.endTime,
      });

      return {
        market,
        marketData: response,
      };
    } catch (error) {
      console.log(`Failed to read candle data for market ${market.questionId}: ${error}`);
      return null;
    }
  });

  const marketDataResults = await Promise.all(marketDataPromises);

  return {
    marketDataResults,
  };
}
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Task Triggers" icon="code" href="../task-triggers">
    Trigger tasks from cron, the CLI and via HTTP
  </Card>

  <Card title="Using Packages" icon="file-code" href="./packages">
    You can use any sandbox compatible typescript packages with any package manager.
  </Card>
</CardGroup>
