Под race condition я подразумеваю такое состояние, когда пользователю отобразилось не то, что он хотел из-за отработки предыдущего запроса, наглядный пример:
https://sebastienlorber.com/handling-api-request-r... в разделе Adding network delays + failures
Возможное решение автора сохранять предыдущий запрос:
// A ref to store the last issued pending request
const lastPromise = useRef();
useEffect(() => {
setData(null);
// fire the api request
const currentPromise = fetchStarwarsHeroData(id).then(
async data => {
await delayRandomly();
throwRandomly();
return data;
},
);
// store the promise to the ref
lastPromise.current = currentPromise;
// handle the result with filtering
currentPromise.then(
result => {
if (currentPromise === lastPromise.current) {
setData(result);
}
},
e => {
if (currentPromise === lastPromise.current) {
console.warn('fetch failure', e);
}
},
);
}, [id]);
В react есть thunk, saga, а как правильнее сделать это во Vue?
Еще пример:
гифка