concurrency-pattern-1-coordinate-async-operations-with-deferred

concurrency-pattern-1-coordinate-async-operations-with-deferred is a cursor rule for coding agents from PaulJPhilp/EffectPatterns. It costs 984 tokens per session, scanned A, original, MIT.

A TypeScript rule for coordinating asynchronous work, where several tasks may need to wait for one task to finish initialization. A Deferred is a one-time result that one task completes and other tasks can wait for.

In plain words
What is it for?
It helps coordinate service startup and other one-time results shared between concurrent workers in Effect-based TypeScript programs.
Why use it?
It prevents each waiting task from starting its own duplicate initialization and gives all consumers a clear signal that a service is ready.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/pauljphilp/effectpatterns/concurrency-pattern-1-coordinate-async-operations-with-deferred
Clone the repo
git clone --depth 1 https://github.com/PaulJPhilp/EffectPatterns

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for concurrency-pattern-1-coordinate-async-operations-with-deferred

README.md
[![agentmods](https://agentmods.dev/badge/rules/pauljphilp/effectpatterns/concurrency-pattern-1-coordinate-async-operations-with-deferred.svg)](https://agentmods.dev/rules/pauljphilp/effectpatterns/concurrency-pattern-1-coordinate-async-operations-with-deferred)
Your own site
<a href="https://agentmods.dev/rules/pauljphilp/effectpatterns/concurrency-pattern-1-coordinate-async-operations-with-deferred"><img src="https://agentmods.dev/badge/rules/pauljphilp/effectpatterns/concurrency-pattern-1-coordinate-async-operations-with-deferred.svg" alt="Measured on agentmods" height="20"></a>
Per session 984 This file is loaded in full into every session.
When invoked 984 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00984 $0.00984
Opus 5 $0.00492 $0.00492
Sonnet 5 $0.00197 $0.00197
Haiku 4.5 $0.00098 $0.00098

Measured 5d ago against content hash 77361555e10a, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-05, from the pricing page.

Security

Grade A, and why

concurrency-pattern-1-coordinate-async-operations-with-deferred scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 5d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

content/published/rules/cursor/concurrency-pattern-1-coordinate-async-operations-with-deferred.mdc · 142 lines

How it starts

The opening of the file, as written. The whole thing — 142 lines — stays where its author put it; the contents beside it link to each section on GitHub.

description: Use Deferred for one-time async coordination between fibers, enabling multiple consumers to wait for a single producer's result. globs: "**/*.ts" alwaysApply: true

Concurrency Pattern 1: Coordinate Async Operations with Deferred

Rule: Use Deferred for one-time async coordination between fibers, enabling multiple consumers to wait for a single producer's result.

Example

This example demonstrates a service startup pattern where multiple workers wait for initialization to complete before starting processing.

import { Effect, Deferred, Fiber } from "effect";

interface ServiceConfig {
  readonly name: string;
  readonly port: number;
}

interface Service {
  readonly name: string;
  readonly isReady: Deferred.Deferred<void>;
}

// Simulate a service that takes time to initialize
const createService = (config: ServiceConfig): Effect.Effect<Service> =>
  Effect.gen(function* () {
    const isReady = yield* Deferred.make<void>();

    return { name: config.name, isReady };
  });

// Initialize the service (runs in background)
const initializeService = (service: Service): Effect.Effect<void> =>
  Effect.gen(function* () {
    yield* Effect.log(`[${service.name}] Starting initialization...`);

    // Simulate initialization work
    yield* Effect.sleep("1 second");

    yield* Effect.log(`[${service.name}] Initialization complete`);

    // Signal that service is ready
    yield* Deferred.succeed(service.isReady, undefined);
  });

// A worker that waits for service to be ready before starting
const createWorker = (
  id: number,
  services: Service[]
): Effect.Effect<void> =>
  Effect.gen(function* () {
    yield* Effect.log(`[Worker ${id}] Starting, waiting for services...`);

    // Wait for all services to be ready
    yield* Effect.all(
      services.map((service) =>
        Deferred.await(service.isReady).pipe(
          Effect.tapError((error) =>
            Effect.log(
              `[Worker ${id}] Error waiting for ${service.name}: ${error}`
            )
          )
        )
      )
    );

    yield* Effect.log(`[Worker ${id}] All services ready, starting work`);

    // Simulate worker processing
    for (let i = 0; i < 3; i++) {
      yield* Effect.sleep("500 millis");
      yield* Effect.log(`[Worker ${id}] Processing task ${i + 1}`);
    }

    yield* Effect.log(`[Worker ${id}] Complete`);
  });

// Main program
const program = Effect.gen(function* () {
  // Create services
  const apiService = yield* createService({ name: "API", port: 3000 });
  const dbService = yield* createService({ name: "Database", port: 5432 });
  const cacheService = yield* createService({ name: "Cache", port: 6379 });

  const services = [apiService, dbService, cacheService];

  // Start initializing services in background
  const initFibers = yield* Effect.all(
    services.map((service) => initializeService(service).pipe(Effect.fork))
  );

  // Start workers that wait for services
  const workerFibers = yield* Effect.all(
    [1, 2, 3].map((id) => createWorker(id, services).pipe(Effect.fork))
  );

  // Wait for all workers to complete
  yield* Effect.all(workerFibers.map((fiber) => Fiber.join(fiber)));

  // Cancel initialization fibers (they're done anyway)
  yield* Effect.all(initFibers.map((fiber) => Fiber.interrupt(fiber)));

  yield* Effect.log(`\n[MAIN] All workers completed`);
});

Effect.runPromise(program);

Read the full file on GitHub · 142 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 5d ago First seen · 142 lines · 984 tokens per session scan A 77361555e10a

Subscribe to this mod's changes

concurrency-pattern-1-coordinate-async-operations-with-deferred is a cursor rule published in the GitHub repository PaulJPhilp/EffectPatterns (796 stars, last pushed 2mo ago), licensed MIT. It adds 984 tokens to every session, about $0.0049 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.