The Fewest Number of Concepts You Need to Use Effect
There are 5 concepts you need to understand to start using Effect.
1. Effect.tryPromise
Think of Effect like a souped-up Promise that’s honest about errors.
// The Promise Version of getUser
try {
const user = await getUser("123")
console.log(user.name)
} catch (error) {
// Is this a network error? 404? 500? Who knows!
console.error("Something went wrong:", error)
}
Versus…
// The Effect Version
import { Effect } from "effect"
Effect.tryPromise({
try: () => getUser(id),
catch: () => `DatabaseError` as const,
});
Since we saved DbError as a TypeScript literal, hovering over this Effect will show us the error at the type level <User, DatabaseError, never>.
The first argument is the success value (the user we wanted), the second argument covers any known errors associated with this “promise” (in this case, a DatabaseError), and the third argument is the...
