Micro for Effect Users
The Micro
module in Effect is designed as a lighter alternative to the standard Effect
module, tailored for situations where it is beneficial to reduce the bundle size.
This module is standalone and does not include more complex functionalities such as Layer
, Ref
, Queue
, and Deferred
. This feature set makes Micro especially suitable for libraries that wish to utilize Effect functionalities while keeping the bundle size to a minimum, particularly for those aiming to provide Promise
-based APIs.
Micro also supports use cases where a client application uses Micro, and a server employs the full suite of Effect features, maintaining both compatibility and logical consistency across various application components.
Integrating Micro adds a minimal footprint to your bundle, starting at 5kb gzipped, which may increase depending on the features you use.
Micro is a part of the Effect library and can be imported just like any other module:
import * as Micro from "effect/Micro"
The Micro
type uses three type parameters:
Micro<Success, Error, Requirements>
which mirror those of the Effect
type.
The MicroExit
type is a streamlined version of the Exit type, designed to capture the outcome of a Micro
computation. It uses the Either data type to distinguish between successful outcomes and failures:
type MicroExit<A, E = never> = Either<A, MicroCause<E>>
The MicroCause
type is a streamlined version of the Cause type.
Similar to how Cause
is a union of types, MicroCause
consists of three specific types:
type MicroCause<E> = Die | Fail<E> | Interrupt
Failure Type | Description |
---|---|
Die | Indicates an unforeseen defect that wasn’t planned for in the system’s logic. |
Fail<E> | Covers anticipated errors that are recognized and typically handled within the application. |
Interrupt | Signifies an operation that has been purposefully stopped. |
The MicroSchedule
type is a streamlined version of the Schedule type.
type MicroSchedule = (attempt: number, elapsed: number) => Option<number>
Represents a function that can be used to calculate the delay between repeats.
The function takes the current attempt number and the elapsed time since
the first attempt, and returns the delay for the next attempt. If the
function returns None
, the repetition will stop.
Below, you’ll find a series of comparisons between the functionalities of Effect
and Micro
. Each table lists a functionality of Effect
alongside its counterpart in Micro
. The icons used have the following meanings:
- ⚠️: The feature is available in
Micro
, but with some differences fromEffect
. - ❌: The feature is not available in
Effect
.
Effect | Micro | |
---|---|---|
Effect.try | ⚠️ Micro.try | requires a try block |
Effect.tryPromise | ⚠️ Micro.tryPromise | requires a try block |
Effect.sleep | ⚠️ Micro.sleep | only handles milliseconds |
Effect.failCause | ⚠️ Micro.failWith | uses MicroCause instead of Cause |
Effect.failCauseSync | ⚠️ Micro.failWithSync | uses MicroCause instead of Cause |
❌ | Micro.make | |
❌ | Micro.fromOption | |
❌ | Micro.fromEither |
Effect | Micro | |
---|---|---|
Effect.runSyncExit | ⚠️ Micro.runSyncExit | returns a MicroExit instead of an Exit |
Effect.runPromiseExit | ⚠️ Micro.runPromiseExit | returns a MicroExit instead of an Exit |
Effect.runFork | ⚠️ Micro.runFork | returns a Handle instead of a RuntimeFiber |
The Micro.runSyncExit
function is used to execute an Effect synchronously, which means it runs immediately and returns the result as a MicroExit.
1import * as import Micro
Micro from "effect/Micro"2
3const const result1: Micro.MicroExit<number, never>
result1 = import Micro
Micro.const runSyncExit: <number, never>(effect: Micro.Micro<number, never, never>) => Micro.MicroExit<number, never>
Attempt to execute the `Micro` effect synchronously and return the `MicroExit`.
If any asynchronous effects are encountered, the function will return a
`CauseDie` containing the `Handle`.
runSyncExit(import Micro
Micro.const succeed: <number>(a: number) => Micro.Micro<number, never, never>
Creates a `Micro` effect that will succeed with the specified constant value.
succeed(1))4namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(const result1: Micro.MicroExit<number, never>
result1)5/*6Output:7{ _id: 'Either', _tag: 'Right', right: 1 }8*/9
10const const result2: Micro.MicroExit<never, string>
result2 = import Micro
Micro.const runSyncExit: <never, string>(effect: Micro.Micro<never, string, never>) => Micro.MicroExit<never, string>
Attempt to execute the `Micro` effect synchronously and return the `MicroExit`.
If any asynchronous effects are encountered, the function will return a
`CauseDie` containing the `Handle`.
runSyncExit(import Micro
Micro.const fail: <string>(e: string) => Micro.Micro<never, string, never>
Creates a `Micro` effect that will fail with the specified error.
This will result in a `CauseFail`, where the error is tracked at the
type level.
fail("my error"))11namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(const result2: Micro.MicroExit<never, string>
result2)12/*13Output:14{ _id: 'Either', _tag: 'Left', left: MicroCause.Fail: my error }15*/
The Micro.runPromiseExit
function is used to execute an Effect and obtain the result as a Promise
that resolves to a MicroExit.
1import * as import Micro
Micro from "effect/Micro"2
3import Micro
Micro.const runPromiseExit: <number, never>(effect: Micro.Micro<number, never, never>, options?: {
readonly signal?: AbortSignal | undefined;
readonly scheduler?: Micro.MicroScheduler | undefined;
} | undefined) => Promise<...>
Execute the `Micro` effect and return a `Promise` that resolves with the
`MicroExit` of the computation.
runPromiseExit(import Micro
Micro.const succeed: <number>(a: number) => Micro.Micro<number, never, never>
Creates a `Micro` effect that will succeed with the specified constant value.
succeed(1)).(method) Promise<MicroExit<number, never>>.then<void, never>(onfulfilled?: ((value: Micro.MicroExit<number, never>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>
Attaches callbacks for the resolution and/or rejection of the Promise.
then(namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log)4/*5Output:6{ _id: 'Either', _tag: 'Right', right: 1 }7*/8
9import Micro
Micro.const runPromiseExit: <never, string>(effect: Micro.Micro<never, string, never>, options?: {
readonly signal?: AbortSignal | undefined;
readonly scheduler?: Micro.MicroScheduler | undefined;
} | undefined) => Promise<...>
Execute the `Micro` effect and return a `Promise` that resolves with the
`MicroExit` of the computation.
runPromiseExit(import Micro
Micro.const fail: <string>(e: string) => Micro.Micro<never, string, never>
Creates a `Micro` effect that will fail with the specified error.
This will result in a `CauseFail`, where the error is tracked at the
type level.
fail("my error")).(method) Promise<MicroExit<never, string>>.then<void, never>(onfulfilled?: ((value: Micro.MicroExit<never, string>) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>
Attaches callbacks for the resolution and/or rejection of the Promise.
then(namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log)10/*11Output:12{ _id: 'Either', _tag: 'Left', left: MicroCause.Fail: my error }13*/
The Micro.runFork
function executes the effect and return a Handle
that can be awaited, joined, or aborted.
You can listen for the result by adding an observer using the handle’s addObserver
method.
1import * as import Micro
Micro from "effect/Micro"2
3const const handle: Micro.Handle<number, never>
handle = import Micro
Micro.const succeed: <number>(a: number) => Micro.Micro<number, never, never>
Creates a `Micro` effect that will succeed with the specified constant value.
succeed(42).(method) Pipeable.pipe<Micro.Micro<number, never, never>, Micro.Micro<number, never, never>, Micro.Handle<number, never>>(this: Micro.Micro<...>, ab: (_: Micro.Micro<number, never, never>) => Micro.Micro<...>, bc: (_: Micro.Micro<...>) => Micro.Handle<...>): Micro.Handle<...> (+21 overloads)
pipe(import Micro
Micro.const delay: (millis: number) => <A, E, R>(self: Micro.Micro<A, E, R>) => Micro.Micro<A, E, R> (+1 overload)
Returns an effect that will delay the execution of this effect by the
specified duration.
delay(1000), import Micro
Micro.const runFork: <A, E>(effect: Micro.Micro<A, E>, options?: {
readonly signal?: AbortSignal | undefined;
readonly scheduler?: Micro.MicroScheduler | undefined;
} | undefined) => Micro.Handle<A, E>
Execute the `Micro` effect and return a `Handle` that can be awaited, joined,
or aborted.
You can listen for the result by adding an observer using the handle's
`addObserver` method.
runFork)4
5const handle: Micro.Handle<number, never>
handle.(property) Handle<number, never>.addObserver: (observer: (exit: Micro.MicroExit<number, never>) => void) => void
addObserver(((parameter) result: Micro.MicroExit<number, never>
result) => {6 namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log((parameter) result: Micro.MicroExit<number, never>
result)7})8namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log("observing...")9/*10Output:11observing...12{ _id: 'Either', _tag: 'Right', right: 42 }13*/
Effect | Micro | |
---|---|---|
Effect.andThen | ⚠️ Micro.andThen | doesn’t handle Promise or () => Promise as argument |
Effect.tap | ⚠️ Micro.tap | doesn’t handle () => Promise as argument |
Effect.all | ⚠️ Micro.all | no batching and mode options |
Effect.forEach | ⚠️ Micro.forEach | no batching option |
Effect.filter | ⚠️ Micro.filter | no batching option |
Effect.filterMap | ⚠️ Micro.filterMap | effectful |
Effect | Micro | |
---|---|---|
Effect.exit | ⚠️ Micro.exit | returns a MicroExit instead of an Exit |
Effect | Micro | |
---|---|---|
❌ | Micro.catchCauseIf |
Effect | Micro | |
---|---|---|
❌ | Micro.timeoutOrElse |
To access a service while using Micro.gen
, you need to wrap the service tag using the Micro.service
function:
1import * as import Context
Context from "effect/Context"2import * as import Micro
Micro from "effect/Micro"3
4class class Random
Random extends import Context
Context.const Tag: <"MyRandomService">(id: "MyRandomService") => <Self, Shape>() => Context.TagClass<Self, "MyRandomService", Shape>
namespace Tag
Tag("MyRandomService")<5 class Random
Random,6 { readonly (property) next: Micro.Micro<number, never, never>
next: import Micro
Micro.interface Micro<out A, out E = never, out R = never>
namespace Micro
A lightweight alternative to the `Effect` data type, with a subset of the functionality.
Micro<number> }7>() {}8
9const const program: Micro.Micro<void, never, Random>
program = import Micro
Micro.const gen: <unknown, YieldWrap<Micro.Micro<{
readonly next: Micro.Micro<number>;
}, never, Random>> | YieldWrap<Micro.Micro<number, never, never>>, void>(...args: [self: ...] | [body: ...]) => Micro.Micro<...>
gen(function* () {10 // const random = yield* Random // this doesn't work11 const const random: {
readonly next: Micro.Micro<number>;
}
random = yield* import Micro
Micro.const service: <Random, {
readonly next: Micro.Micro<number>;
}>(tag: Context.Tag<Random, {
readonly next: Micro.Micro<number>;
}>) => Micro.Micro<{
readonly next: Micro.Micro<number>;
}, never, Random>
Access the given `Context.Tag` from the environment.
service(class Random
Random)12 const const randomNumber: number
randomNumber = yield* const random: {
readonly next: Micro.Micro<number>;
}
random.(property) next: Micro.Micro<number, never, never>
next13 namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log(`random number: ${const randomNumber: number
randomNumber}`)14})15
16const const runnable: Micro.Micro<void, never, never>
runnable = import Micro
Micro.const provideService: <void, never, Random, Random, {
next: Micro.Micro<number, never, never>;
}>(self: Micro.Micro<void, never, Random>, tag: Context.Tag<Random, {
next: Micro.Micro<...>;
}>, service: {
next: Micro.Micro<...>;
}) => Micro.Micro<...> (+1 overload)
Add the provided service to the current context.
provideService(const program: Micro.Micro<void, never, Random>
program, class Random
Random, {17 (property) next: Micro.Micro<number, never, never>
next: import Micro
Micro.const sync: <number>(evaluate: LazyArg<number>) => Micro.Micro<number, never, never>
Creates a `Micro` effect that will succeed with the lazily evaluated value.
If the evaluation of the value throws an error, the effect will fail with
`CauseDie`.
sync(() => var Math: Math
An intrinsic object that provides basic mathematics functionality and constants.
Math.(method) Math.random(): number
Returns a pseudorandom number between 0 and 1.
random())18})19
20import Micro
Micro.const runPromise: <void, never>(effect: Micro.Micro<void, never, never>, options?: {
readonly signal?: AbortSignal | undefined;
readonly scheduler?: Micro.MicroScheduler | undefined;
} | undefined) => Promise<...>
Execute the `Micro` effect and return a `Promise` that resolves with the
successful value of the computation.
runPromise(const runnable: Micro.Micro<void, never, never>
runnable)21/*22Example Output:23random number: 0.824187223313441724*/
Effect | Micro | |
---|---|---|
Scope | ⚠️ MicroScope | returns a MicroScope instead of a Scope |
Scope.make | ⚠️ Micro.scopeMake | returns a MicroScope instead of a Scope |
1import * as import Micro
Micro from "effect/Micro"2
3const const consoleLog: (message: string) => Micro.Micro<void, never, never>
consoleLog = ((parameter) message: string
message: string) =>4 import Micro
Micro.const sync: <void>(evaluate: LazyArg<void>) => Micro.Micro<void, never, never>
Creates a `Micro` effect that will succeed with the lazily evaluated value.
If the evaluation of the value throws an error, the effect will fail with
`CauseDie`.
sync(() => namespace console
var console: Console
The `console` module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.
The module exports two specific components:
* A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream.
* A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and
[`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module.
_**Warning**_: The global console object's methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for
more information.
Example using the global `console`:
```js
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
```
Example using the `Console` class:
```js
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
```
console.(method) Console.log(message?: any, ...optionalParams: any[]): void
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.
log((parameter) message: string
message))5
6const const program: Micro.Micro<void, never, never>
program =7 // create a new scope8 import Micro
Micro.const scopeMake: Micro.Micro<Micro.MicroScope.Closeable, never, never>
scopeMake.(method) Pipeable.pipe<Micro.Micro<Micro.MicroScope.Closeable, never, never>, Micro.Micro<Micro.MicroScope.Closeable, never, never>, Micro.Micro<Micro.MicroScope.Closeable, never, never>, Micro.Micro<...>>(this: Micro.Micro<...>, ab: (_: Micro.Micro<...>) => Micro.Micro<...>, bc: (_: Micro.Micro<...>) => Micro.Micro<...>, cd: (_: Micro.Micro<...>) => Micro.Micro<...>): Micro.Micro<...> (+21 overloads)
pipe(9 // add finalizer 110 import Micro
Micro.const tap: <Micro.MicroScope.Closeable, Micro.Micro<void, never, never>>(f: (a: Micro.MicroScope.Closeable) => Micro.Micro<void, never, never>) => <E, R>(self: Micro.Micro<...>) => Micro.Micro<...> (+3 overloads)
Execute a side effect from the success value of the `Micro` effect.
It is similar to the `andThen` api, but the success value is ignored.
tap(((parameter) scope: Micro.MicroScope.Closeable
scope) =>11 (parameter) scope: Micro.MicroScope.Closeable
scope.(property) MicroScope.addFinalizer: (finalizer: (exit: Micro.MicroExit<unknown, unknown>) => Micro.Micro<void>) => Micro.Micro<void>
addFinalizer(() => const consoleLog: (message: string) => Micro.Micro<void, never, never>
consoleLog("finalizer 1"))12 ),13 // add finalizer 214 import Micro
Micro.const tap: <Micro.MicroScope.Closeable, Micro.Micro<void, never, never>>(f: (a: Micro.MicroScope.Closeable) => Micro.Micro<void, never, never>) => <E, R>(self: Micro.Micro<...>) => Micro.Micro<...> (+3 overloads)
Execute a side effect from the success value of the `Micro` effect.
It is similar to the `andThen` api, but the success value is ignored.
tap(((parameter) scope: Micro.MicroScope.Closeable
scope) =>15 (parameter) scope: Micro.MicroScope.Closeable
scope.(property) MicroScope.addFinalizer: (finalizer: (exit: Micro.MicroExit<unknown, unknown>) => Micro.Micro<void>) => Micro.Micro<void>
addFinalizer(() => const consoleLog: (message: string) => Micro.Micro<void, never, never>
consoleLog("finalizer 2"))16 ),17 // close the scope18 import Micro
Micro.const andThen: <Micro.MicroScope.Closeable, Micro.Micro<void, never, never>>(f: (a: Micro.MicroScope.Closeable) => Micro.Micro<void, never, never>) => <E, R>(self: Micro.Micro<...>) => Micro.Micro<...> (+3 overloads)
A more flexible version of `flatMap`, that combines `map` and `flatMap` into
a single api.
It also allows you to pass in a `Micro` effect directly, which will be
executed after the current effect.
andThen(((parameter) scope: Micro.MicroScope.Closeable
scope) =>19 (parameter) scope: Micro.MicroScope.Closeable
scope.(property) MicroScope.Closeable.close: (exit: Micro.MicroExit<any, any>) => Micro.Micro<void>
close(import Micro
Micro.const exitSucceed: <string>(a: string) => Micro.MicroExit<string, never>
exitSucceed("scope closed successfully"))20 )21 )22
23import Micro
Micro.const runPromise: <void, never>(effect: Micro.Micro<void, never, never>, options?: {
readonly signal?: AbortSignal | undefined;
readonly scheduler?: Micro.MicroScheduler | undefined;
} | undefined) => Promise<...>
Execute the `Micro` effect and return a `Promise` that resolves with the
successful value of the computation.
runPromise(const program: Micro.Micro<void, never, never>
program)24/*25Output:26finalizer 2 <-- finalizers are closed in reverse order27finalizer 128*/
Effect | Micro | |
---|---|---|
Effect.retry | ⚠️ Micro.retry | different options |
Effect | Micro | |
---|---|---|
Effect.repeat | ⚠️ Micro.repeat | different options |
❌ (Effect.exit + Effect.repeat ) | ⚠️ Micro.repeatExit |
Effect | Micro | |
---|---|---|
❌ | Micro.timeoutOrElse |
Effect | Micro | |
---|---|---|
Effect.sandbox | ⚠️ Micro.sandbox | MicroCause<E> instead of Cause<E> |
Effect | Micro | |
---|---|---|
❌ | Micro.filterOrFailWith | |
Effect.tapErrorCause | ⚠️ Micro.tapErrorCause | MicroCause<E> instead of Cause<E> |
❌ | Micro.tapCauseIf | |
Effect.tapDefect | ⚠️ Micro.tapDefect | unknown instead of Cause<never> |
Effect | Micro | |
---|---|---|
Effect.provide | ⚠️ Micro.provideContext | only handles Context |
❌ | Micro.provideScope | |
❌ | Micro.service |
Effect | Micro | |
---|---|---|
Effect.addFinalizer | ⚠️ Micro.addFinalizer | MicroExit instead of Exit and no R |
Effect.acquireRelease | ⚠️ Micro.acquireRelease | MicroExit instead of Exit |
Effect.acquireUseRelease | ⚠️ Micro.acquireUseRelease | MicroExit instead of Exit |
Effect.onExit | ⚠️ Micro.onExit | MicroExit instead of Exit |
Effect.onError | ⚠️ Micro.onError | uses MicroCause instead of Cause |
❌ | Micro.onExitIf |
Effect | Micro | |
---|---|---|
Effect.fork | ⚠️ Micro.fork | Handle instead of RuntimeFiber |
Effect.forkDaemon | ⚠️ Micro.forkDaemon | Handle instead of RuntimeFiber |
Effect.forkIn | ⚠️ Micro.forkIn | Handle instead of RuntimeFiber |
Effect.forkScoped | ⚠️ Micro.forkScoped | Handle instead of RuntimeFiber |