09. Promise Patterns

Control abstractions:

  • p-if - execute then handler if match some predicate.

  • p-catch-if - evaluate predicate on error type at catch block.

  • p-whilst - async version of the while statement.

  • p-do-whilst - async version of the do…while statement.

  • p-forever - async version of while (true) {}.

  • p-wait-for - wait for a condition to be true.

  • 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-reflect - promise always fulfill with its actual fulfillment value or rejection reason.

    • p-settle - same as p-reflect but for array of promises.

  • p-times - run async function a specific number of times concurrently.

Timings:

  • delay - resolve promise with delay.

  • nanodelay - A tiny (28 bytes) Promise wrapper around setTimeout.

  • 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 - think setImmediate().

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 as Promise.all(), but accepts functions instead of promises directly so you can limit the concurrency.

  • p-race - fixes the silly behavior of Promise.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 of Promise.race().

  • p-some - use when you need the fastest of multiple promises.

  • p-every - like Array.every for promises.

  • p-one - like Array.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 as Promise.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-pipe - compose async functions into a reusable pipeline.

  • p-waterfall - run async functions in series, each passing its result to the next.

  • p-props - like Promise.all() but for Map and Object.

  • 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

Rejections

Last updated