09. Promise Patterns
Control abstractions:
p-if
- executethen
handler if match some predicate.p-catch-if
- evaluate predicate on error type atcatch
block.p-whilst
- async version of thewhile
statement.p-do-whilst
- async version of thedo…while
statement.p-forever
- async version ofwhile (true) {}
.p-wait-for
- wait for a condition to betrue
.p-finally
-Promise.finally()
.p-try
-Promise.try()
.
Execution strategies:
p-retry
- retry async function.p-cancellable
- create a promise that can be canceled.p-progress
- create a promise that reports progress.p-times
- run async function a specific number of times concurrently.
Timings:
delay
- resolve promise with delay.nanodelay
- A tiny (28 bytes) Promise wrapper aroundsetTimeout
.p-min-delay
- delay a promise a minimum amount of time.p-timeout
- reject promise after a specified amount of time.p-immediate
- returns a promise resolved in the next event loop - thinksetImmediate()
.
Executors:
p-map
- run async functions multiple times with different inputs concurrently.p-filter
- run async functions multiple times with different inputs concurrently and get a filtered down result.p-reduce
- reduce a list of values using promises into a promise for a value.p-all
- same asPromise.all()
, but accepts functions instead of promises directly so you can limit the concurrency.p-race
- fixes the silly behavior ofPromise.race()
returning a forever pending promise. when supplied an empty iterable, which could create some really hard to debug problems.p-any
- return the fastest promise. Use this instead ofPromise.race()
.p-some
- use when you need the fastest of multiple promises.p-every
- likeArray.every
for promises.p-one
- likeArray.some
for promises.p-locate
- get the first fulfilled promise that satisfies the provided testing function.p-limit
- run multiple async functions with limited concurrency.Bluebird:
Promise.join
- same asPromise.all()
, but for fixed amount of discrete promises that you want to coordinate concurrently (more performant).
Promise data/execution structures:
p-queue
- promise queue with concurrency control.p-series
- run async functions in series.p-map-series
- serialPromise.all
with mapper function.p-each-series
- iterate over promises serially.
p-pipe
- compose async functions into a reusable pipeline.p-waterfall
- run async functions in series, each passing its result to the next.p-props
- likePromise.all()
but forMap
andObject
.mux
- creates a promise that waits for the promises in nested data structures and resolves to data structures of the same form. It recursively traverses the input data structure and multiplexes its promises.
Functional Programming
p-debounce
- debounce async functions.p-throttle
- throttle async functions.p-memoize
- memoize promises caching the result of calls with identical input.reuse-promise
- reuse the same promise that's returned from a function until it's resolved.p-lazy
- create a lazy promise that defers execution until.then()
or.catch()
is called.
Utils:
pify
- promisify callback or CPS style functions.p-event
- promisify event (once).p-tap
- access to promise value without chaining promise.p-log
- log the value/error of a promise.p-time
- measure the time a promise takes to resolve.Bluebird:
Promise.method
- wraps the given function into new function. The new function will always return a promise that is fulfilled with the original functions return values or rejected with thrown exceptions from the original function.
Error Handling
await-to-js
- convert Promise to[err, data]
outcome.catchify
- various error handling strategies for Promise..disposer
, objects that wrap a resource and a method to release that resource, together with.Promise.using
, a function to safely use disposers in a way that automatically calls their release method.
Rejections
Last updated
Was this helpful?