AgentScript Concurrency Examples
AgentScript
function fetchUsersConcurrently(ids: number[]): Array<User> goal: "Fetch a list of users with controlled concurrency." backstory: "We want to retrieve multiple user records in parallel, but limit how many requests run at once." requires: ids.length > 0: "Must provide at least one ID." concurrency: 3
return: // The `concurrentForEach` here represents a parallel fetch with concurrency=3 concurrentForEach(ids, (id) => getUser(id)) |> tap(users => Console.log("Got users", users))
errors: FetchError: "Failed to fetch one or more users." // Additional error signaling or checks could go here
tests: success: // Provide a small array of IDs and expect a valid result [1, 2, 3] -> [ { id: 1, ... }, { id: 2, ... }, { id: 3, ... } ] concurrency: // Example concurrency test with 10 IDs. The test runner // will ensure only 3 at a time execute concurrently. input: [0..9] -> expect "No concurrency conflict / correct results" performance: // We might expect it to run within some timeframe input: [0..9] -> expect < 200ms
TypeScript (Effect)
import { Console, Effect } from "effect"
declare const getUser: ( id: number,) => Effect.Effect<unknown, Error>
const ids = Array.from( { length: 10 }, (_, i) => i,)
const main = Effect.forEach( ids, (id) => getUser(id), { concurrency: 3 },).pipe( Effect.andThen((users) => Console.log("Got users", users), ),)
Effect.runPromise(main)
TypeScript (Plain)
declare const getUser: ( id: number,) => Promise<unknown>
function forEach<A, B>( items: Array<A>, concurrency: number, f: (a: A) => Promise<B>,): Promise<Array<B>> { let index = 0 const results: Array<B> = new Array( items.length, ) async function process(index: number) { const next = items[index] results[index] = await f(next) } async function worker() { while (index < items.length) { await process(index++) } } return Promise.all( Array.from({ length: concurrency }, worker), ).then(() => results)}
const ids = Array.from( { length: 10 }, (_, i) => i,)
async function main() { const users = await forEach(ids, 3, (id) => getUser(id), ) console.log("Got users", users)}
main()