Resources represent cloud infrastructure — buckets, queues, functions,
databases, and so on. Each resource is yield*-ed inside the Stack’s
Effect generator.
Let’s add a Cloudflare R2 Bucket to our Stack and observe the type error:
This layer provides no services and can be used as a neutral element
in layer composition or as a starting point for building layers.
@example
import { Layer } from"effect"
constemptyLayer= Layer.empty
@since ― 2.0.0
@category ― constructors
empty,
Error ts(2322) ― Type 'Layer<never, never, never>' is not assignable to type 'Layer<NoInfer<Providers>, never, StackServices>'.
Type 'Cloudflare.Providers' is not assignable to type 'never'.
Provides a way to write effectful code using generator functions, simplifying
control flow and error handling.
When to Use
gen allows you to write code that looks and behaves like synchronous
code, but it can handle asynchronous tasks, errors, and complex control flow
(like loops and conditions). It helps make asynchronous code more readable
and easier to manage.
The generator functions work similarly to async/await but with more
explicit control over the execution of effects. You can yield* values from
effects and return the final result at the end.
Replace Layer.empty with Cloudflare.providers() to resolve the
type error:
alchemy.run.ts
import*as Alchemy from"alchemy";
import*as Cloudflare from"alchemy/Cloudflare";
import*as Effect from"effect/Effect";
import*as Layer from"effect/Layer";
exportdefaultAlchemy.Stack(
"MyApp",
{
providers: Layer.empty,
providers: Cloudflare.providers(),
},
Effect.gen(function* () {
constbucket=yield* Cloudflare.R2Bucket("Bucket");
}),
);
Now the program type-checks. The providers layer tells Alchemy how to
talk to Cloudflare’s APIs, and the type system ensures you never
forget to wire it up.
Provides a way to write effectful code using generator functions, simplifying
control flow and error handling.
When to Use
gen allows you to write code that looks and behaves like synchronous
code, but it can handle asynchronous tasks, errors, and complex control flow
(like loops and conditions). It helps make asynchronous code more readable
and easier to manage.
The generator functions work similarly to async/await but with more
explicit control over the execution of effects. You can yield* values from
effects and return the final result at the end.