Skip to content

Commit 75e6b39

Browse files
committed
Add example snippet for limiting query concurrency
1 parent 2885f69 commit 75e6b39

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

docs/rtk-query/usage/customizing-queries.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,25 @@ export const { useGetPostsQuery } = api
838838
*/
839839
```
840840

841+
### Limiting Request Concurrency
842+
843+
One approach for limiting simultaneous outstanding requests is to write a small wrapper around your base query that uses a promise queue library like `p-limit`, such as this example:
844+
845+
```ts
846+
import pLimit from 'p-limit'
847+
import { fetchBaseQuery } from '@reduxjs/toolkit/query'
848+
849+
const baseQuery = fetchBaseQuery({ baseUrl: '/api' })
850+
851+
const limit = pLimit(10)
852+
853+
const baseQueryWithLimit: typeof baseQuery = (arg, api, extraOptions) => {
854+
// if you wanted to run mutations immediately, for example
855+
if (api.type === 'mutation') return baseQuery(arg, api, extraOptions)
856+
return limit(baseQuery, arg, api, extraOptions)
857+
}
858+
```
859+
841860
## Examples - `transformResponse`
842861

843862
### Unpacking deeply nested GraphQL data

0 commit comments

Comments
 (0)