File: asyncThrottle.md | Updated: 11/15/2025
Search...
+ K
Auto
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples GitHub Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Docs Examples Github Contributors
Maintainers Partners Support Learn StatsBETA Discord Merch Blog GitHub Ethos Brand Guide
Documentation
Framework
React
Version
Latest
Search...
+ K
Menu
Getting Started
Guides
API Reference
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference
Debouncer Examples
Throttler Examples
Rate Limiter Examples
Queue Examples
Batcher Examples
TanStack Query Examples
Framework
React
Version
Latest
Menu
Getting Started
Guides
API Reference
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference
Debouncer Examples
Throttler Examples
Rate Limiter Examples
Queue Examples
Batcher Examples
TanStack Query Examples
React Example: AsyncThrottle
=============================================================================================================================================================================================================================================================================================================================================================================================================================
Code ExplorerCode
Interactive SandboxSandbox
public
src
.eslintrc.cjs
.gitignore
README.md
index.html
package.json
tsconfig.json
vite.config.ts
tsx
import { useCallback, useState } from 'react'
import ReactDOM from 'react-dom/client'
import { asyncThrottle } from '@tanstack/react-pacer/async-throttler'
function SearchApp() {
const [searchText, setSearchText] = useState('')
const [throttledSearchText, setThrottledSearchText] = useState('')
const [searchResults, setSearchResults] = useState<Array<string>>([])
const [loading, setLoading] = useState(false)
// Simulate search API
const simulateSearch = async (query: string) => {
await new Promise((resolve) => setTimeout(resolve, 800))
return [\
`Result 1 for ${query}`,\
`Result 2 for ${query}`,\
`Result 3 for ${query}`,\
]
}
const throttledSetSearch = useCallback(
asyncThrottle(
async (value: string) => {
try {
setLoading(true)
setThrottledSearchText(value)
const results = await simulateSearch(value)
setSearchResults(results)
} catch (err) {
setSearchResults([])
} finally {
setLoading(false)
}
},
{
wait: 1000,
},
),
[], // must be memoized to avoid re-creating the throttler on every render (consider using useAsyncThrottler instead in react)
)
return (
<div>
<h1>TanStack Pacer asyncThrottle Example</h1>
<div>
<input
autoFocus
type="search"
value={searchText}
onChange={(e) => {
const newValue = e.target.value
setSearchText(newValue)
throttledSetSearch(newValue)
}}
placeholder="Type to search..."
style={{ width: '100%' }}
/>
{loading && <div>Loading...</div>}
</div>
<table>
<tbody>
<tr>
<td>Instant Search:</td>
<td>{searchText}</td>
</tr>
<tr>
<td>Throttled Search:</td>
<td>{throttledSearchText}</td>
</tr>
</tbody>
</table>
<div>
<h3>Search Results:</h3>
<ul>
{searchResults.map((result, i) => (
<li key={i}>{result}</li>
))}
</ul>
</div>
</div>
)
}
const root = ReactDOM.createRoot(document.getElementById('root')!)
root.render(<SearchApp />)
import { useCallback, useState } from 'react'
import ReactDOM from 'react-dom/client'
import { asyncThrottle } from '@tanstack/react-pacer/async-throttler'
function SearchApp() {
const [searchText, setSearchText] = useState('')
const [throttledSearchText, setThrottledSearchText] = useState('')
const [searchResults, setSearchResults] = useState<Array<string>>([])
const [loading, setLoading] = useState(false)
// Simulate search API
const simulateSearch = async (query: string) => {
await new Promise((resolve) => setTimeout(resolve, 800))
return [\
`Result 1 for ${query}`,\
`Result 2 for ${query}`,\
`Result 3 for ${query}`,\
]
}
const throttledSetSearch = useCallback(
asyncThrottle(
async (value: string) => {
try {
setLoading(true)
setThrottledSearchText(value)
const results = await simulateSearch(value)
setSearchResults(results)
} catch (err) {
setSearchResults([])
} finally {
setLoading(false)
}
},
{
wait: 1000,
},
),
[], // must be memoized to avoid re-creating the throttler on every render (consider using useAsyncThrottler instead in react)
)
return (
<div>
<h1>TanStack Pacer asyncThrottle Example</h1>
<div>
<input
autoFocus
type="search"
value={searchText}
onChange={(e) => {
const newValue = e.target.value
setSearchText(newValue)
throttledSetSearch(newValue)
}}
placeholder="Type to search..."
style={{ width: '100%' }}
/>
{loading && <div>Loading...</div>}
</div>
<table>
<tbody>
<tr>
<td>Instant Search:</td>
<td>{searchText}</td>
</tr>
<tr>
<td>Throttled Search:</td>
<td>{throttledSearchText}</td>
</tr>
</tbody>
</table>
<div>
<h3>Search Results:</h3>
<ul>
{searchResults.map((result, i) => (
<li key={i}>{result}</li>
))}
</ul>
</div>
</div>
)
}
const root = ReactDOM.createRoot(document.getElementById('root')!)
root.render(<SearchApp />)
