DEV Community

Discussion on: Practical data fetching with React Suspense that you can use today

ruinerwarrior profile image
Rick Nijhuis

Nice article!, one question though, wouldn't it be simpler and maybe more performant if you just passed the promise instead of a method returning a promise? per example:

const promise = (() => { const resp = await fetch("http://url"); return resp.json })()

No need for caching the request because it's already executed and doesn't need to be executed inside the function.

andreiduca profile image
Andrei Author

Calling fetch immediately will start the api call immediately. But data needs to be retrieved on demand, and usually with parameters (fetch a user by its id). You cannot create fetch promises for all possible IDs you don't know you're going to use.

Also, we're not caching the fetch requests (i.e. the functions that return the promises), but the generated data reader functions (i.e. the functions that just return the data, or throw the in-progress promises).