Navigate
Search topics, hooks, and fundamentals
JavaScript is single-threaded but non-blocking. Think of it like a restaurant — one waiter, but the kitchen works in parallel.
💡 Understanding async patterns is essential for data fetching, useEffect cleanup, and preventing race conditions in React.
These are the patterns that trip up developers most often. Switch between Wrong and Fixed to compare the code side by side.
// async callback returns a Promise, not a cleanup function
useEffect(async () => {
const res = await fetch("/api/data");
const data = await res.json();
setData(data);
}, []);// No protection against out-of-order responses
useEffect(() => {
async function search() {
const results = await fetchResults(query);
setResults(results); // might be from an older query!
}
search();
}, [query]);// If fetchData rejects, error is swallowed entirely
useEffect(() => {
async function load() {
const data = await fetchData(); // unhandled rejection!
setData(data);
}
load();
}, []);