Skip to main content

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 and calling it with fetch(), or by using the callTask() function. Typically you’ll want to use callTask().
callTask<Args = Record<string, unknown>, T = unknown>(
  taskName: string,
  args: Args,
  retryConfig?: ContextFunctionRetryConfig,
): Promise<T>

Basic Example

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

import { TaskContext } from "compose";

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

export async function main({ logEvent, 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) {
      await logEvent({
        code: "PAST_DUE_MARKET_DATA_READ_FAILED",
        message: `Failed to read candle data for market ${market.questionId}: ${error}`,
        data: JSON.stringify({
          questionId: market.questionId,
          error: String(error),
        }),
      });
      return null;
    }
  });

  const marketDataResults = await Promise.all(marketDataPromises);

  return {
    marketDataResults,
  };
}

Next Steps