Composable primitives for asynchronous operations.
Provides utilities for coordinating concurrent operations and managing execution flow.
Usage
import { sleep, Mutex, Throttle } from '@metreeca/core/async';// Simple delayasync function retry(task: () => Promise<void>) { for (let i = 0; i < 3; i++) { try { await task(); break; } catch (error) { await sleep(1000 * Math.pow(2, i)); // Exponential backoff: 1s, 2s, 4s } }}// Prevent race conditionsconst mutex = Mutex();let counter = 0;async function increment() { await mutex.execute(async () => { const current = counter; await sleep(10); // Simulate async read counter = current + 1; // Safe write - no race condition });}// Rate limit API callsconst throttle = Throttle({ minimum: 100, // At least 100ms between requests backoff: 2.0, // Double delay on failure recover: 0.5 // Halve delay on success});async function fetchData(url: string) { await throttle.queue(true); try { const response = await fetch(url); await throttle.adapt(true); // Success - speed up return response; } catch (error) { await throttle.adapt(false); // Failure - slow down throw error; }}// Automatic retry with throttlingconst result = await throttle.retry( () => fetch('https://api.example.com/data'), { attempts: 5, recover: (error) => { // Retry on network errors, skip on 4xx client errors if (error instanceof TypeError) return 0; // Use default backoff if (error.status >= 500) return 1000; // Retry after 1s return undefined; // Don't retry } }); Copy
import { sleep, Mutex, Throttle } from '@metreeca/core/async';// Simple delayasync function retry(task: () => Promise<void>) { for (let i = 0; i < 3; i++) { try { await task(); break; } catch (error) { await sleep(1000 * Math.pow(2, i)); // Exponential backoff: 1s, 2s, 4s } }}// Prevent race conditionsconst mutex = Mutex();let counter = 0;async function increment() { await mutex.execute(async () => { const current = counter; await sleep(10); // Simulate async read counter = current + 1; // Safe write - no race condition });}// Rate limit API callsconst throttle = Throttle({ minimum: 100, // At least 100ms between requests backoff: 2.0, // Double delay on failure recover: 0.5 // Halve delay on success});async function fetchData(url: string) { await throttle.queue(true); try { const response = await fetch(url); await throttle.adapt(true); // Success - speed up return response; } catch (error) { await throttle.adapt(false); // Failure - slow down throw error; }}// Automatic retry with throttlingconst result = await throttle.retry( () => fetch('https://api.example.com/data'), { attempts: 5, recover: (error) => { // Retry on network errors, skip on 4xx client errors if (error instanceof TypeError) return 0; // Use default backoff if (error.status >= 500) return 1000; // Retry after 1s return undefined; // Don't retry } });
Mutual exclusion primitive for coordinating asynchronous operations.
Adaptive throttle for rate-limiting concurrent task execution.
Creates a mutual exclusion primitive for coordinating asynchronous operations.
Asynchronously pauses execution for the specified duration.
Creates an adaptive throttle for rate-limiting concurrent task execution.
Composable primitives for asynchronous operations.
Provides utilities for coordinating concurrent operations and managing execution flow.
Usage