@nfd/ptl
v0.1.4
Published
Promise tools
Downloads
9
Readme
ptl /ˈpɛdl/
Promise tools.
Install
npm i @nfd/ptl
Use
const ptl = require('@nfd/ptl')
API
limit(…) serial(…) dual(…) try(…) sleep(…) immediate(…) any(…) all(…) race(…)
limit(n, xs)
Resolves to an array of the results of the items of xs
, which should be [a promise of] a synchronous or asynchronous iterator or iterable of promises or values, maintaining a maximum of n
unsettled promises before waiting for some to settle. Rejects with the error if any item rejects.
ptl.limit(3, itt.map(['/array', '/of', '/some', '/paths', ...], async p =>
JSON.parse(await collect(request(`https://${DOMAIN}${p}`)))))
serial(xs)
Resolves to an array of the results of the items of xs
, which should be [a promise of] a synchronous or asynchronous iterator or iterable of promises or values, only taking one unsettled promise at a time. Rejects with the error if any item rejects. Equivalent to limit(1, xs)
dual(promise)
Resolves to the error if promise
rejects; rejects with the result if promise
resolves.
try(fn)
Resolves to the result of fn()
; rejects if fn()
throws a synchronous error.
sleep(ms)
Resolves to undefined
after ms
milliseconds.
immediate()
Resolves after a setImmediate
cycle.
any(promises)
Resolves to the result of the first of promises
to resolve, or rejects with an array of all errors. The dual
of all
all(promises)
Alias for Promise.all
, but accepts a promise of an iterable.
race(promises)
Alias for Promise.race
, but accepts a promise of an iterable.