I'm trying to incrementally fetch episodes from an array, so the user experience would look like this:
A user clicks a button, shows a loading indicator, loads the first episode fetched inside a div, but keeps showing the loading indicator below that first episode, keeps doing this until the map is over and all the episodes are fetched and the loading indicator is not shown anymore.
But apparently, this does not appear to be working as planned, I'm suspecting that async-await does not work as expected inside a map.
I'm wondering if something like rxjs
would help in this use case(I have no idea how it works, but I know it is used in cases where the data is streaming)
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');
const [data, setData] = useState([]);
const handleClick = async () => {
try {
season.episodes.map(async (episode) => {
setIsLoading(true);
const links = await publicQueryClientApi(
`${process.env.NEXT_PUBLIC_SERVER_URL}/download/${episode.slug}`
);
setIsLoading(false);
setData((prev) => [...prev, { episode, links }]);
setError('');
});
} catch (error) {
setIsLoading(false);
setData([]);
setError(error.message);
}
};