Let's come back to our initial plain typescript solution:
const main = async () => {
const response = await fetch("https://pokeapi.co/api/v2/pokemon/garchomp/");
const json = await response.json();
return json;
};
main().then(console.log);
There is a problem here: fetch
works even when the response status is not 200 (okay).
We therefore need to handle this situation manually:
const main = async () => {
const response = await fetch("https://pokeapi.co/api/v2/pokemon/garchomp/");
if (!response.ok) {
throw new Error("Response not okay");
}
const json = await response.json();
return json;
};
Let's do the same in effect!
Pipe operations and effect API
Now, bear this in mind: the effect API is large.
That's by design: the API allows you to handle any use case (for example filtering, timeouts, retries, and more). However, explaining every single function in a course would make it boring and way too long.
Two solutions for this:
- Use the IDE autosuggestions to search the API you need. From there you can read the function documentation, read the type signature, or go directly to the source implementation. All without leaving your IDE.
With effect going-to-source from your IDE won't bring you to a
.d.ts
file, but instead to the actual original source code implementation.Most APIs are simple enough to read and understand in isolation.
- Go to the API reference and search the service or function that you need.
You can also ask for help on the Effect's Discord Community, I strongly suggest you to join 👍
My suggestion is always to work on a real project and find what you need along the way.
If your use case is common always assume that it is covered by effect.
Want to filter? Search
filter
. Want to map?map
, Want to timeout?timeout
.As a general rule avoid custom implementations of common functions which become harder to maintain.
That's exactly what we are doing in this course: we have a specific goal (make an API request) and we are discovering the API that we need as we implement more features.
Expect to be searching the API frequently as you get started with effect.