apiplex
v1.0.0
Published
Async API-call multiplexer: run many tasks with a concurrency cap, aggregate results, tolerate failures. Zero dependencies.
Maintainers
Readme
apiplex
A zero-dependency async multiplexer for API calls and other async tasks. Run many tasks with a concurrency cap, aggregate results by name, tolerate individual failures, and rate-limit callers.
Pricing
Free. MIT-licensed, open source. No fees, no tiers.
Install
npm install apiplexUsage
const { multiplex, runAll, rateLimited } = require("apiplex");
// Fan out several API calls, run at most 3 at a time, keep going on failures
const { results, errors } = await multiplex(
{
users: () => fetch("/users").then(r => r.json()),
posts: () => fetch("/posts").then(r => r.json()),
stats: () => fetch("/stats").then(r => r.json()),
},
{ concurrency: 3, onError: (err, name) => console.error(name, err.message) }
);
console.log(results.users, results.posts); // per-key results
if (errors.stats) { /* that one failed but others completed */ }
// Or: run all, fail fast on the first rejection
const all = await runAll({ a: () => ..., b: () => ... }, 2);
// Rate-guard a function (max 10 calls/second)
const guarded = rateLimited(expensiveFn, { rate: 10, windowMs: 1000 });
guarded(); // ... excess calls within the window return undefinedAPI
multiplex(tasks, { concurrency, breakOnError, onError })→{ results, errors }runAll(tasks, concurrency?)→results(rejects on first failure)rateLimited(fn, { rate, windowMs })→ guarded fn
License
MIT © shinnjr
