DEV Community

Alex Spinov
Alex Spinov

Posted on

Effect-TS Has a Free API That Most Developers Dont Know About

Effect is a powerful TypeScript library for building reliable applications with typed errors, concurrency, and dependency injection.

Basic Effect

import { Effect, Console } from "effect";

const program = Effect.gen(function* () {
  yield* Console.log("Starting...");
  const result = yield* fetchUser("123");
  return result;
});

Effect.runPromise(program);
Enter fullscreen mode Exit fullscreen mode

Typed Errors

class UserNotFound { readonly _tag = "UserNotFound"; }
class NetworkError { readonly _tag = "NetworkError"; }

const fetchUser = (id: string): Effect.Effect<User, UserNotFound | NetworkError> =>
  Effect.gen(function* () {
    const res = yield* Effect.tryPromise({
      try: () => fetch(`/api/users/${id}`),
      catch: () => new NetworkError()
    });
    if (!res.ok) yield* Effect.fail(new UserNotFound());
    return yield* Effect.tryPromise({ try: () => res.json(), catch: () => new NetworkError() });
  });
Enter fullscreen mode Exit fullscreen mode

Dependency Injection

import { Context, Layer } from "effect";

class Database extends Context.Tag("Database")<Database, {
  readonly query: (sql: string) => Effect.Effect<any[]>;
}>() {}

const PostgresLayer = Layer.succeed(Database, {
  query: (sql) => Effect.succeed([])
});
Enter fullscreen mode Exit fullscreen mode

Key Features

  • Typed errors — never miss an error case
  • Structured concurrency with fibers
  • Compile-time dependency injection
  • Built-in retries, scheduling, observability

Need to scrape or monitor web data at scale? Check out my web scraping actors on Apify or email spinov001@gmail.com for custom solutions.

Top comments (0)