p-dynamic
v0.0.1
Published
Dynamic versions of `Promise.any` and `Promise.race` — add promises after creation.
Maintainers
Readme
p-dynamic
Dynamic versions of Promise.any and Promise.race — add promises after creation.
Installation
npm install p-dynamicQuick Start
pany
Resolves with the first fulfilled value. Rejects with AggregateError only when every added promise has rejected — same semantics as Promise.any, but you can keep adding promises after the fact.
import { pany } from 'p-dynamic'
const { add, promise } = pany<string>()
add(fetch('/api/a').then(r => r.text()))
add(fetch('/api/b').then(r => r.text()))
// later, add more before the first one settles
add(fetch('/api/c').then(r => r.text()))
const result = await promise // first to fulfill winsprace
Settles with the first promise to resolve or reject — same semantics as Promise.race, but dynamic.
import { prace } from 'p-dynamic'
const { add, promise } = prace<string>()
add(fetchWithTimeout('/api/a', 500))
add(fetchWithTimeout('/api/b', 500))
const result = await promise // first to settle (resolve or reject) winsConcept: dynamic promise aggregation
The standard Promise.any / Promise.race require all inputs upfront. p-dynamic lets you register promises incrementally — useful when you don't know all candidates at the start, or when you want to launch additional attempts lazily.
add() returns false once the aggregated promise has already settled, so you can safely call it at any time without side effects.
License
MIT License
