> For the complete documentation index, see [llms.txt](https://strctr.gitbook.io/programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://strctr.gitbook.io/programming/01-languages/javascript/01-language/e-controls/e4-promises/09-promises-patterns.md).

# 09. Promise Patterns

* [x] [`promise-fun` by sindresorhus](https://github.com/sindresorhus/promise-fun)
* [x] [Decorating Async Javascript Functions](http://innolitics.com/10x/javascript-decorators-for-promise-returning-functions/)
* [x] [`bluebird` API reference](http://bluebirdjs.com/docs/api-reference.html)

## Control abstractions:

* [`p-if`](https://github.com/sindresorhus/p-if) - execute `then` handler if match some predicate.
* [`p-catch-if`](https://github.com/sindresorhus/p-catch-if) - evaluate predicate on error type at `catch` block.
* [`p-whilst`](https://github.com/sindresorhus/p-whilst) - async version of the `while` statement.
* [`p-do-whilst`](https://github.com/sindresorhus/p-do-whilst) - async version of the `do…while` statement.
* [`p-forever`](https://github.com/sindresorhus/p-forever) - async version of `while (true) {}`.
* [`p-wait-for`](https://github.com/sindresorhus/p-wait-for) - wait for a condition to be `true`.
* [`p-finally`](https://github.com/sindresorhus/p-finally) - `Promise.finally()`.
* [`p-try`](https://github.com/sindresorhus/p-try) - `Promise.try()`.

## Execution strategies:

* [`p-retry`](https://github.com/sindresorhus/p-retry) - retry async function.
* [`p-cancellable`](https://github.com/sindresorhus/p-cancelable) - create a promise that can be canceled.
* [`p-progress`](https://github.com/sindresorhus/p-progress) - create a promise that reports progress.
* [`p-reflect`](https://github.com/sindresorhus/p-reflect) - promise always fulfill with its actual fulfillment value or rejection reason.
  * [`p-settle`](https://github.com/sindresorhus/p-settle) - same as `p-reflect` but for array of promises.
* [`p-times`](https://github.com/sindresorhus/p-times) - run async function a specific number of times concurrently.

## Timings:

* [`delay`](https://github.com/sindresorhus/delay) - resolve promise with delay.
* [`nanodelay`](https://github.com/ai/nanodelay) - A tiny (28 bytes) Promise wrapper around `setTimeout`.
* [`p-min-delay`](https://github.com/sindresorhus/p-min-delay) - delay a promise a minimum amount of time.
* [`p-timeout`](https://github.com/sindresorhus/p-timeout) - reject promise after a specified amount of time.
* [`p-immediate`](https://github.com/sindresorhus/p-immediate) - returns a promise resolved in the next event loop - think `setImmediate()`.

## Executors:

* [`p-map`](https://github.com/sindresorhus/p-map) - run async functions multiple times with different inputs concurrently.
* [`p-filter`](https://github.com/sindresorhus/p-filter) - run async functions multiple times with different inputs concurrently and get a filtered down result.
* [`p-reduce`](https://github.com/sindresorhus/p-reduce) - reduce a list of values using promises into a promise for a value.
* [`p-all`](https://github.com/sindresorhus/p-all) - same as `Promise.all()`, but accepts functions instead of promises directly so you can limit the concurrency.
* [`p-race`](https://github.com/sindresorhus/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`](https://github.com/sindresorhus/p-any) - return the fastest promise. Use this instead of `Promise.race()`.
* [`p-some`](https://github.com/sindresorhus/p-some) - use when you need the fastest of multiple promises.
* [`p-every`](https://github.com/kevva/p-every) - like `Array.every` for promises.
* [`p-one`](https://github.com/kevva/p-one) - like `Array.some` for promises.
* [`p-locate`](https://github.com/sindresorhus/p-locate) - get the first fulfilled promise that satisfies the provided testing function.
* [`p-limit`](https://github.com/sindresorhus/p-limit) - run multiple async functions with limited concurrency.
* [Bluebird: `Promise.join`](http://bluebirdjs.com/docs/api/promise.join.html#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`](https://github.com/sindresorhus/p-queue) - promise queue with concurrency control.
* [`p-series`](https://github.com/sindresorhus/p-series) - run async functions in series.
  * [`p-map-series`](https://github.com/sindresorhus/p-map-series) - serial `Promise.all` with mapper function.
  * [`p-each-series`](https://github.com/sindresorhus/p-each-series) - iterate over promises serially.
* [`p-pipe`](https://github.com/sindresorhus/p-pipe) - compose async functions into a reusable pipeline.
* [`p-waterfall`](https://github.com/sindresorhus/p-waterfall) - run async functions in series, each passing its result to the next.
* [`p-props`](https://github.com/sindresorhus/p-props) - like `Promise.all()` but for `Map` and `Object`.
* [`mux`](https://github.com/ide/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`](https://github.com/sindresorhus/p-debounce) - debounce async functions.
* [`p-throttle`](https://github.com/sindresorhus/p-throttle) - throttle async functions.
* [`p-memoize`](https://github.com/sindresorhus/p-memoize) - memoize promises caching the result of calls with identical input.
* [`reuse-promise`](https://github.com/elado/reuse-promise) - reuse the same promise that's returned from a function until it's resolved.
* [`p-lazy`](https://github.com/sindresorhus/p-lazy) - create a lazy promise that defers execution until `.then()` or `.catch()` is called.

## Utils:

* [`pify`](https://github.com/sindresorhus/pify) - promisify callback or CPS style functions.
* [`p-event`](https://github.com/sindresorhus/p-event) - promisify event (once).
* [`p-tap`](https://github.com/sindresorhus/p-tap) - access to promise value without chaining promise.
* [`p-log`](https://github.com/sindresorhus/p-log) - log the value/error of a promise.
* [`p-time`](https://github.com/sindresorhus/p-time) - measure the time a promise takes to resolve.
* [Bluebird: `Promise.method`](http://bluebirdjs.com/docs/api/promise.method.html#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`](https://github.com/scopsy/await-to-js) - convert Promise to `[err, data]` outcome.
* [`catchify`](https://github.com/majgis/catchify) - various error handling strategies for Promise.
* [Bluebird: filtered `catch`](http://bluebirdjs.com/docs/api/catch.html#filtered-catch)
* [Bluebird: `.error`](http://bluebirdjs.com/docs/api/catch.html#filtered-catch)
* [Bluebird:  Resource management](http://bluebirdjs.com/docs/api/resource-management.html#resource-management)
  * [`.disposer`](http://bluebirdjs.com/docs/api/disposer.html), objects that wrap a resource and a method to release that resource, together with.
  * [`Promise.using`](http://bluebirdjs.com/docs/api/promise.using.html#promise.using), a function to safely use disposers in a way that automatically calls their release method.

## Rejections

* [`loud-rejection`](https://github.com/sindresorhus/loud-rejection)
* [`hard-rejection`](https://github.com/sindresorhus/hard-rejection)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://strctr.gitbook.io/programming/01-languages/javascript/01-language/e-controls/e4-promises/09-promises-patterns.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
