question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support parallel fetches

See original GitHub issue

The 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:open
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, May 18, 2022

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:

function User({ id }) {
  const name = nameStore.get(id);
  const age = ageStore.get(id);

  return (
    <>
      <p>Name: {name}</p>
      <p>Age: {age}</p>
    </>
  );
}
1reaction
axelboccommented, May 18, 2022

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 with get. A getAll that does this internally would be convenient.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found