Skip to content

Commit

Permalink
Merge pull request #9 from labcodes/feature/promise-all-support
Browse files Browse the repository at this point in the history
Feature/promise all support
  • Loading branch information
lucianoratamero committed Oct 28, 2019
2 parents c4e4e9d + 35a0028 commit 171094d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ That will:
- trigger `FETCH_PRODUCTS_SUCCESS` if the request succeeds;
- trigger `FETCH_PRODUCTS_FAILURE` if it doesn't.


#### Making multiple requests at the same time

If you want to make multiple requests in the same call, instead of returning a single `fetchFromApi` call, you may use [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) passing a list of `fetchFromApi` calls:

```js
export const fetchMultipleProducts = () => {
return {
types: {
request: 'FETCH_MULTIPLE_PRODUCTS',
success: 'FETCH_MULTIPLE_PRODUCTS_SUCCESS',
failure: 'FETCH_MULTIPLE_PRODUCTS_FAILURE',
},
apiCallFunction: () => Promise.all([ fetchFromApi('/api/inventory/1'), fetchFromApi('/api/inventory/2'), ])
};
}
```

When all of them are successful, the `'FETCH_MULTIPLE_PRODUCTS_SUCCESS'` reducer will be called with a list of `Response` objects. When one of them fails, the `'FETCH_MULTIPLE_PRODUCTS_FAILURE'` will be triggered passing the `Error` instance (most probably a `TypeError: Failed to fetch` error).


#### Setup

Assuming you've already installed react and redux, to use it, you'll need to install `redux-thunk` first:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-redux-api-tools",
"version": "2.0.1",
"version": "2.0.2",
"description": "Middleware and helpers to improve the React-Redux flow when communicating with APIs.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 171094d

Please sign in to comment.