Support parallel fetches
See original GitHub issueThe only way to do parallel fetches right now is to render two components as siblings and fetch the data there. Could this library support a getAll()
helper that allows fetching multiple requests across the same store? What about multiple stores?
Single store could look something like this:
const userIds = [1,2,3];
const [user1, user2, user3] = getAll(userStore, userIds);
Multiple stores is a little more difficult. An API like this is a little less ergonomic than the above but would cover both cases:
const [foo, bar, baz] = getAll(
() => fooStore.get(fooId),
() => barStore.get(barId),
() => bazStore.get(bazId),
);
An implementation that I got working looks like this, although I’m not sure of it’s correctness
export const getAll = (...fns) => {
const results = fns.map((fn) => {
let result;
try {
result = { type: "ok", data: fn() };
} catch (promise) {
result = { type: "err", promise };
}
return result;
});
const data = [];
for (const result of results) {
if (result.type === "ok") {
data.push(result.data);
} else {
throw result.promise;
}
}
return data;
};
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Resolving multiple fetch request in parallel - Stack Overflow
I am trying to do a multiple parallel fetch requests in react-native . But i don't get the response data as expected.
Read more >How To Make Parallel API calls in React Applications - Medium
In this post, we will see how to make parallel API calls in React applications using Fetch and Axios. You can do API...
Read more >How to make multiple API requests in parallel? - RapidAPI
You can use fetch Web API to call RESTful and GraphQL APIs. In this piece, let's look at how we can send the...
Read more >Programmatically fetch multiple APIs in parallel using async ...
When I was building ethfolio, I had to figure out how to retrieve the token information for multiple Ethereum addresses.
Read more >Parallel models fetch support : IDEA-262896 - YouTrack
Since version 6.8 Gradle supports fetching TAPI models in parallel using the org.gradle.tooling.BuildController#run API. IntelliJ can leverage the api to ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Prefetching in an event handler is ideal, but doing in render works (I believe it’s safe.) Doing in an effect seems worse.
If your
getAll
doesn’t prefetch in render, and you could prefetch all in an event handler, it can be as simple as this:Yeah, the downside of this is that the requests are made sequentially, if I’m not mistaken, since the component suspends each time. The workaround I found for the single-store case is to do two consecutive loops: one with
prefect
and one withget
. AgetAll
that does this internally would be convenient.