# Introduction

## Installation

First, install the middleware.

```
npm i redux-promise-middleware -s
```

## Setup

Import the middleware and include it in `applyMiddleware` when creating the Redux store:

```javascript
import promise from 'redux-promise-middleware'

composeStoreWithMiddleware = applyMiddleware(
  promise,
)(createStore)
```

## Use

Dispatch a promise as the value of the `payload` property of the action.

```javascript
const foo = () => ({
  type: 'FOO',
  payload: new Promise()
});
```

A pending action is immediately dispatched.

```javascript
{
  type: 'FOO_PENDING'
}
```

Once the promise is settled, a second action will be dispatched. If the promise is resolved a fulfilled action is dispatched.

```javascript
{
  type: 'FOO_FULFILLED'
  payload: {
    ...
  }
}
```

On the other hand, if the promise is rejected, a rejected action is dispatched.

```javascript
{
  type: 'FOO_REJECTED'
  error: true,
  payload: {
    ...
  }
}
```

That's it!

## Further Reading

* [Catching Errors Thrown by Rejected Promises](/redux-promise-middleware/guides/rejected-promises.md)
* [Use with Reducers](/redux-promise-middleware/guides/reducers.md)
* [Optimistic Updates](/redux-promise-middleware/guides/optimistic-updates.md)
* [Design Principles](/redux-promise-middleware/getting-started/design-principles.md)

Copyright (c) 2017 Patrick Burtchaell. [Code licensed with the MIT License (MIT)](https://github.com/pburtchaell/redux-promise-middleware/tree/612043ca69fd1856bc52f308ae92bfd5e9b5c91c/LICENSE/README.md). [Documentation licensed with the CC BY-NC License](https://github.com/pburtchaell/redux-promise-middleware/tree/612043ca69fd1856bc52f308ae92bfd5e9b5c91c/docs/LICENSE/README.md).


---

# Agent Instructions: 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:

```
GET https://pburtchaell.gitbook.io/redux-promise-middleware/getting-started/introduction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
