effect 3.13.9 → 3.13.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Arbitrary.js +38 -22
- package/dist/cjs/Arbitrary.js.map +1 -1
- package/dist/cjs/Array.js +50 -50
- package/dist/cjs/Array.js.map +1 -1
- package/dist/cjs/Brand.js +9 -4
- package/dist/cjs/Brand.js.map +1 -1
- package/dist/cjs/Cause.js +1 -0
- package/dist/cjs/Cause.js.map +1 -1
- package/dist/cjs/Chunk.js +25 -23
- package/dist/cjs/Chunk.js.map +1 -1
- package/dist/cjs/Config.js +2 -1
- package/dist/cjs/Config.js.map +1 -1
- package/dist/cjs/Context.js +4 -4
- package/dist/cjs/Effect.js +823 -632
- package/dist/cjs/Effect.js.map +1 -1
- package/dist/cjs/Either.js +12 -12
- package/dist/cjs/Either.js.map +1 -1
- package/dist/cjs/LogLevel.js +1 -1
- package/dist/cjs/Logger.js +12 -12
- package/dist/cjs/Match.js +26 -26
- package/dist/cjs/Option.js +43 -43
- package/dist/cjs/Predicate.js +6 -2
- package/dist/cjs/Predicate.js.map +1 -1
- package/dist/cjs/Schema.js +21 -17
- package/dist/cjs/Schema.js.map +1 -1
- package/dist/cjs/SchemaAST.js +49 -25
- package/dist/cjs/SchemaAST.js.map +1 -1
- package/dist/cjs/Scope.js +2 -0
- package/dist/cjs/Scope.js.map +1 -1
- package/dist/cjs/Stream.js +93 -93
- package/dist/cjs/internal/core.js +1 -1
- package/dist/cjs/internal/core.js.map +1 -1
- package/dist/cjs/internal/schema/util.js +23 -14
- package/dist/cjs/internal/schema/util.js.map +1 -1
- package/dist/cjs/internal/version.js +1 -1
- package/dist/cjs/internal/version.js.map +1 -1
- package/dist/dts/Array.d.ts +47 -47
- package/dist/dts/Array.d.ts.map +1 -1
- package/dist/dts/Brand.d.ts +14 -6
- package/dist/dts/Brand.d.ts.map +1 -1
- package/dist/dts/Cause.d.ts +1 -0
- package/dist/dts/Cause.d.ts.map +1 -1
- package/dist/dts/Chunk.d.ts +91 -89
- package/dist/dts/Chunk.d.ts.map +1 -1
- package/dist/dts/Config.d.ts +2 -1
- package/dist/dts/Config.d.ts.map +1 -1
- package/dist/dts/Context.d.ts +4 -4
- package/dist/dts/Effect.d.ts +2107 -1621
- package/dist/dts/Effect.d.ts.map +1 -1
- package/dist/dts/Either.d.ts +33 -33
- package/dist/dts/LogLevel.d.ts +3 -3
- package/dist/dts/Logger.d.ts +16 -16
- package/dist/dts/Match.d.ts +26 -26
- package/dist/dts/Option.d.ts +84 -84
- package/dist/dts/Predicate.d.ts.map +1 -1
- package/dist/dts/Schema.d.ts +53 -21
- package/dist/dts/Schema.d.ts.map +1 -1
- package/dist/dts/SchemaAST.d.ts.map +1 -1
- package/dist/dts/Scope.d.ts +2 -0
- package/dist/dts/Scope.d.ts.map +1 -1
- package/dist/dts/Stream.d.ts +229 -229
- package/dist/esm/Arbitrary.js +38 -22
- package/dist/esm/Arbitrary.js.map +1 -1
- package/dist/esm/Array.js +50 -50
- package/dist/esm/Array.js.map +1 -1
- package/dist/esm/Brand.js +9 -4
- package/dist/esm/Brand.js.map +1 -1
- package/dist/esm/Cause.js +1 -0
- package/dist/esm/Cause.js.map +1 -1
- package/dist/esm/Chunk.js +25 -23
- package/dist/esm/Chunk.js.map +1 -1
- package/dist/esm/Config.js +2 -1
- package/dist/esm/Config.js.map +1 -1
- package/dist/esm/Context.js +4 -4
- package/dist/esm/Effect.js +845 -648
- package/dist/esm/Effect.js.map +1 -1
- package/dist/esm/Either.js +17 -17
- package/dist/esm/Either.js.map +1 -1
- package/dist/esm/LogLevel.js +1 -1
- package/dist/esm/Logger.js +12 -12
- package/dist/esm/Match.js +26 -26
- package/dist/esm/Option.js +48 -48
- package/dist/esm/Predicate.js +3 -0
- package/dist/esm/Predicate.js.map +1 -1
- package/dist/esm/Schema.js +21 -17
- package/dist/esm/Schema.js.map +1 -1
- package/dist/esm/SchemaAST.js +46 -22
- package/dist/esm/SchemaAST.js.map +1 -1
- package/dist/esm/Scope.js +2 -0
- package/dist/esm/Scope.js.map +1 -1
- package/dist/esm/Stream.js +101 -101
- package/dist/esm/internal/core.js +1 -1
- package/dist/esm/internal/core.js.map +1 -1
- package/dist/esm/internal/schema/util.js +23 -14
- package/dist/esm/internal/schema/util.js.map +1 -1
- package/dist/esm/internal/version.js +1 -1
- package/dist/esm/internal/version.js.map +1 -1
- package/package.json +1 -1
- package/src/Arbitrary.ts +39 -24
- package/src/Array.ts +116 -113
- package/src/Brand.ts +14 -6
- package/src/Cause.ts +1 -0
- package/src/Chunk.ts +97 -95
- package/src/Config.ts +2 -1
- package/src/Context.ts +4 -4
- package/src/Effect.ts +2107 -1621
- package/src/Either.ts +34 -34
- package/src/LogLevel.ts +3 -3
- package/src/Logger.ts +16 -16
- package/src/Match.ts +26 -26
- package/src/Option.ts +84 -84
- package/src/Predicate.ts +4 -0
- package/src/Schema.ts +87 -41
- package/src/SchemaAST.ts +47 -22
- package/src/Scope.ts +2 -0
- package/src/Stream.ts +229 -229
- package/src/internal/core.ts +2 -1
- package/src/internal/schema/util.ts +34 -25
- package/src/internal/version.ts +1 -1
package/dist/esm/Effect.js
CHANGED
|
@@ -62,12 +62,8 @@ export const isEffect = core.isEffect;
|
|
|
62
62
|
* By caching the result, you can improve efficiency and reduce unnecessary
|
|
63
63
|
* computations, especially in performance-critical applications.
|
|
64
64
|
*
|
|
65
|
-
*
|
|
66
|
-
* indefinitely.
|
|
67
|
-
* @see {@link cachedInvalidateWithTTL} for a similar function that includes an
|
|
68
|
-
* additional effect for manually invalidating the cached value.
|
|
65
|
+
* **Example**
|
|
69
66
|
*
|
|
70
|
-
* @example
|
|
71
67
|
* ```ts
|
|
72
68
|
* import { Effect, Console } from "effect"
|
|
73
69
|
*
|
|
@@ -89,7 +85,7 @@ export const isEffect = core.isEffect;
|
|
|
89
85
|
* yield* cached.pipe(Effect.andThen(Console.log))
|
|
90
86
|
* })
|
|
91
87
|
*
|
|
92
|
-
*
|
|
88
|
+
* Effect.runFork(program)
|
|
93
89
|
* // Output:
|
|
94
90
|
* // expensive task...
|
|
95
91
|
* // result 1
|
|
@@ -98,6 +94,11 @@ export const isEffect = core.isEffect;
|
|
|
98
94
|
* // result 2
|
|
99
95
|
* ```
|
|
100
96
|
*
|
|
97
|
+
* @see {@link cached} for a similar function that caches the result
|
|
98
|
+
* indefinitely.
|
|
99
|
+
* @see {@link cachedInvalidateWithTTL} for a similar function that includes an
|
|
100
|
+
* additional effect for manually invalidating the cached value.
|
|
101
|
+
*
|
|
101
102
|
* @since 2.0.0
|
|
102
103
|
* @category Caching
|
|
103
104
|
*/
|
|
@@ -129,12 +130,8 @@ export const cachedWithTTL = circular.cached;
|
|
|
129
130
|
* a certain period but still want to invalidate it if the underlying data
|
|
130
131
|
* changes or if you want to force a recomputation.
|
|
131
132
|
*
|
|
132
|
-
*
|
|
133
|
-
* indefinitely.
|
|
134
|
-
* @see {@link cachedWithTTL} for a similar function that caches the result for
|
|
135
|
-
* a specified duration but does not include an effect for manual invalidation.
|
|
133
|
+
* **Example**
|
|
136
134
|
*
|
|
137
|
-
* @example
|
|
138
135
|
* ```ts
|
|
139
136
|
* import { Effect, Console } from "effect"
|
|
140
137
|
*
|
|
@@ -159,7 +156,7 @@ export const cachedWithTTL = circular.cached;
|
|
|
159
156
|
* yield* cached.pipe(Effect.andThen(Console.log))
|
|
160
157
|
* })
|
|
161
158
|
*
|
|
162
|
-
*
|
|
159
|
+
* Effect.runFork(program)
|
|
163
160
|
* // Output:
|
|
164
161
|
* // expensive task...
|
|
165
162
|
* // result 1
|
|
@@ -168,6 +165,11 @@ export const cachedWithTTL = circular.cached;
|
|
|
168
165
|
* // result 2
|
|
169
166
|
* ```
|
|
170
167
|
*
|
|
168
|
+
* @see {@link cached} for a similar function that caches the result
|
|
169
|
+
* indefinitely.
|
|
170
|
+
* @see {@link cachedWithTTL} for a similar function that caches the result for
|
|
171
|
+
* a specified duration but does not include an effect for manual invalidation.
|
|
172
|
+
*
|
|
171
173
|
* @since 2.0.0
|
|
172
174
|
* @category Caching
|
|
173
175
|
*/
|
|
@@ -190,12 +192,8 @@ export const cachedInvalidateWithTTL = circular.cachedInvalidateWithTTL;
|
|
|
190
192
|
* and all following evaluations will immediately return the cached value,
|
|
191
193
|
* improving performance and reducing unnecessary work.
|
|
192
194
|
*
|
|
193
|
-
*
|
|
194
|
-
* time-to-live duration for the cached value.
|
|
195
|
-
* @see {@link cachedInvalidateWithTTL} for a similar function that includes an
|
|
196
|
-
* additional effect for manually invalidating the cached value.
|
|
195
|
+
* **Example**
|
|
197
196
|
*
|
|
198
|
-
* @example
|
|
199
197
|
* ```ts
|
|
200
198
|
* import { Effect, Console } from "effect"
|
|
201
199
|
*
|
|
@@ -219,7 +217,7 @@ export const cachedInvalidateWithTTL = circular.cachedInvalidateWithTTL;
|
|
|
219
217
|
* yield* cached.pipe(Effect.andThen(Console.log))
|
|
220
218
|
* })
|
|
221
219
|
*
|
|
222
|
-
*
|
|
220
|
+
* Effect.runFork(program)
|
|
223
221
|
* // Output:
|
|
224
222
|
* // non-cached version:
|
|
225
223
|
* // expensive task...
|
|
@@ -232,6 +230,11 @@ export const cachedInvalidateWithTTL = circular.cachedInvalidateWithTTL;
|
|
|
232
230
|
* // result 3
|
|
233
231
|
* ```
|
|
234
232
|
*
|
|
233
|
+
* @see {@link cachedWithTTL} for a similar function that includes a
|
|
234
|
+
* time-to-live duration for the cached value.
|
|
235
|
+
* @see {@link cachedInvalidateWithTTL} for a similar function that includes an
|
|
236
|
+
* additional effect for manually invalidating the cached value.
|
|
237
|
+
*
|
|
235
238
|
* @since 2.0.0
|
|
236
239
|
* @category Caching
|
|
237
240
|
*/
|
|
@@ -262,7 +265,8 @@ export const cached = effect.memoize;
|
|
|
262
265
|
* expensive calculations or operations that should be avoided after the first
|
|
263
266
|
* execution with the same parameters.
|
|
264
267
|
*
|
|
265
|
-
*
|
|
268
|
+
* **Example**
|
|
269
|
+
*
|
|
266
270
|
* ```ts
|
|
267
271
|
* import { Effect, Random } from "effect"
|
|
268
272
|
*
|
|
@@ -278,7 +282,7 @@ export const cached = effect.memoize;
|
|
|
278
282
|
* console.log(yield* memoized(10))
|
|
279
283
|
* })
|
|
280
284
|
*
|
|
281
|
-
*
|
|
285
|
+
* Effect.runFork(program)
|
|
282
286
|
* // Example Output:
|
|
283
287
|
* // non-memoized version:
|
|
284
288
|
* // 2
|
|
@@ -310,7 +314,8 @@ export const cachedFunction = circular.cachedFunction;
|
|
|
310
314
|
* have initialization tasks, logging, or other one-time actions that should not
|
|
311
315
|
* be repeated. This can help optimize performance and avoid redundant actions.
|
|
312
316
|
*
|
|
313
|
-
*
|
|
317
|
+
* **Example**
|
|
318
|
+
*
|
|
314
319
|
* ```ts
|
|
315
320
|
* import { Effect, Console } from "effect"
|
|
316
321
|
*
|
|
@@ -321,7 +326,7 @@ export const cachedFunction = circular.cachedFunction;
|
|
|
321
326
|
* yield* Effect.repeatN(task2, 2)
|
|
322
327
|
* })
|
|
323
328
|
*
|
|
324
|
-
*
|
|
329
|
+
* Effect.runFork(program)
|
|
325
330
|
* // Output:
|
|
326
331
|
* // task1
|
|
327
332
|
* // task1
|
|
@@ -380,12 +385,9 @@ export const once = effect.once;
|
|
|
380
385
|
* success or failure. Each effect returns `None` for success and `Some` with
|
|
381
386
|
* the error for failure.
|
|
382
387
|
*
|
|
383
|
-
*
|
|
384
|
-
* @see {@link allWith} for a data-last version of this function.
|
|
388
|
+
* **Example** (Combining Effects in Tuples)
|
|
385
389
|
*
|
|
386
|
-
* @example
|
|
387
390
|
* ```ts
|
|
388
|
-
* // Title: Combining Effects in Tuples
|
|
389
391
|
* import { Effect, Console } from "effect"
|
|
390
392
|
*
|
|
391
393
|
* const tupleOfEffects = [
|
|
@@ -397,15 +399,16 @@ export const once = effect.once;
|
|
|
397
399
|
* // ▼
|
|
398
400
|
* const resultsAsTuple = Effect.all(tupleOfEffects)
|
|
399
401
|
*
|
|
400
|
-
*
|
|
402
|
+
* Effect.runPromise(resultsAsTuple).then(console.log)
|
|
401
403
|
* // Output:
|
|
402
404
|
* // 42
|
|
403
405
|
* // Hello
|
|
404
406
|
* // [ 42, 'Hello' ]
|
|
405
407
|
* ```
|
|
406
408
|
*
|
|
407
|
-
*
|
|
408
|
-
*
|
|
409
|
+
* **Example** (Combining Effects in Iterables)
|
|
410
|
+
*
|
|
411
|
+
* ```ts
|
|
409
412
|
* import { Effect, Console } from "effect"
|
|
410
413
|
*
|
|
411
414
|
* const iterableOfEffects: Iterable<Effect.Effect<number>> = [1, 2, 3].map(
|
|
@@ -416,15 +419,17 @@ export const once = effect.once;
|
|
|
416
419
|
* // ▼
|
|
417
420
|
* const resultsAsArray = Effect.all(iterableOfEffects)
|
|
418
421
|
*
|
|
419
|
-
*
|
|
422
|
+
* Effect.runPromise(resultsAsArray).then(console.log)
|
|
420
423
|
* // Output:
|
|
421
424
|
* // 1
|
|
422
425
|
* // 2
|
|
423
426
|
* // 3
|
|
424
427
|
* // [ 1, 2, 3 ]
|
|
428
|
+
* ```
|
|
429
|
+
*
|
|
430
|
+
* **Example** (Combining Effects in Structs)
|
|
425
431
|
*
|
|
426
|
-
*
|
|
427
|
-
* // Title: Combining Effects in Structs
|
|
432
|
+
* ```ts
|
|
428
433
|
* import { Effect, Console } from "effect"
|
|
429
434
|
*
|
|
430
435
|
* const structOfEffects = {
|
|
@@ -436,14 +441,16 @@ export const once = effect.once;
|
|
|
436
441
|
* // ▼
|
|
437
442
|
* const resultsAsStruct = Effect.all(structOfEffects)
|
|
438
443
|
*
|
|
439
|
-
*
|
|
444
|
+
* Effect.runPromise(resultsAsStruct).then(console.log)
|
|
440
445
|
* // Output:
|
|
441
446
|
* // 42
|
|
442
447
|
* // Hello
|
|
443
448
|
* // { a: 42, b: 'Hello' }
|
|
449
|
+
* ```
|
|
450
|
+
*
|
|
451
|
+
* **Example** (Combining Effects in Records)
|
|
444
452
|
*
|
|
445
|
-
*
|
|
446
|
-
* // Title: Combining Effects in Records
|
|
453
|
+
* ```ts
|
|
447
454
|
* import { Effect, Console } from "effect"
|
|
448
455
|
*
|
|
449
456
|
* const recordOfEffects: Record<string, Effect.Effect<number>> = {
|
|
@@ -455,14 +462,16 @@ export const once = effect.once;
|
|
|
455
462
|
* // ▼
|
|
456
463
|
* const resultsAsRecord = Effect.all(recordOfEffects)
|
|
457
464
|
*
|
|
458
|
-
*
|
|
465
|
+
* Effect.runPromise(resultsAsRecord).then(console.log)
|
|
459
466
|
* // Output:
|
|
460
467
|
* // 1
|
|
461
468
|
* // 2
|
|
462
469
|
* // { key1: 1, key2: 2 }
|
|
470
|
+
* ```
|
|
471
|
+
*
|
|
472
|
+
* **Example** (Short-Circuiting Behavior)
|
|
463
473
|
*
|
|
464
|
-
*
|
|
465
|
-
* // Title: Short-Circuiting Behavior
|
|
474
|
+
* ```ts
|
|
466
475
|
* import { Effect, Console } from "effect"
|
|
467
476
|
*
|
|
468
477
|
* const program = Effect.all([
|
|
@@ -472,7 +481,7 @@ export const once = effect.once;
|
|
|
472
481
|
* Effect.succeed("Task3").pipe(Effect.tap(Console.log))
|
|
473
482
|
* ])
|
|
474
483
|
*
|
|
475
|
-
*
|
|
484
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
476
485
|
* // Output:
|
|
477
486
|
* // Task1
|
|
478
487
|
* // {
|
|
@@ -480,9 +489,11 @@ export const once = effect.once;
|
|
|
480
489
|
* // _tag: 'Failure',
|
|
481
490
|
* // cause: { _id: 'Cause', _tag: 'Fail', failure: 'Task2: Oh no!' }
|
|
482
491
|
* // }
|
|
492
|
+
* ```
|
|
483
493
|
*
|
|
484
|
-
*
|
|
485
|
-
*
|
|
494
|
+
* **Example** (Collecting Results with `mode: "either"`)
|
|
495
|
+
*
|
|
496
|
+
* ```ts
|
|
486
497
|
* import { Effect, Console } from "effect"
|
|
487
498
|
*
|
|
488
499
|
* const effects = [
|
|
@@ -493,7 +504,7 @@ export const once = effect.once;
|
|
|
493
504
|
*
|
|
494
505
|
* const program = Effect.all(effects, { mode: "either" })
|
|
495
506
|
*
|
|
496
|
-
*
|
|
507
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
497
508
|
* // Output:
|
|
498
509
|
* // Task1
|
|
499
510
|
* // Task3
|
|
@@ -506,9 +517,11 @@ export const once = effect.once;
|
|
|
506
517
|
* // { _id: 'Either', _tag: 'Right', right: 'Task3' }
|
|
507
518
|
* // ]
|
|
508
519
|
* // }
|
|
520
|
+
* ```
|
|
509
521
|
*
|
|
510
|
-
*
|
|
511
|
-
*
|
|
522
|
+
* **Example** (Collecting Results with `mode: "validate"`)
|
|
523
|
+
*
|
|
524
|
+
* ```ts
|
|
512
525
|
* import { Effect, Console } from "effect"
|
|
513
526
|
*
|
|
514
527
|
* const effects = [
|
|
@@ -519,7 +532,7 @@ export const once = effect.once;
|
|
|
519
532
|
*
|
|
520
533
|
* const program = Effect.all(effects, { mode: "validate" })
|
|
521
534
|
*
|
|
522
|
-
*
|
|
535
|
+
* Effect.runPromiseExit(program).then((result) => console.log("%o", result))
|
|
523
536
|
* // Output:
|
|
524
537
|
* // Task1
|
|
525
538
|
* // Task3
|
|
@@ -536,6 +549,10 @@ export const once = effect.once;
|
|
|
536
549
|
* // ]
|
|
537
550
|
* // }
|
|
538
551
|
* // }
|
|
552
|
+
* ```
|
|
553
|
+
*
|
|
554
|
+
* @see {@link forEach} for iterating over elements and applying an effect.
|
|
555
|
+
* @see {@link allWith} for a data-last version of this function.
|
|
539
556
|
*
|
|
540
557
|
* @since 2.0.0
|
|
541
558
|
* @category Collecting
|
|
@@ -550,7 +567,8 @@ export const all = fiberRuntime.all;
|
|
|
550
567
|
* options such as concurrency levels. This version is useful in functional
|
|
551
568
|
* pipelines where you first define your data and then apply operations to it.
|
|
552
569
|
*
|
|
553
|
-
*
|
|
570
|
+
* **Example**
|
|
571
|
+
*
|
|
554
572
|
* ```ts
|
|
555
573
|
* import { Effect, pipe } from "effect"
|
|
556
574
|
*
|
|
@@ -570,7 +588,7 @@ export const all = fiberRuntime.all;
|
|
|
570
588
|
* Effect.allWith({ concurrency: 2 })
|
|
571
589
|
* )
|
|
572
590
|
*
|
|
573
|
-
*
|
|
591
|
+
* Effect.runPromise(program).then(console.log)
|
|
574
592
|
* // Output:
|
|
575
593
|
* // timestamp=... level=INFO fiber=#3 message="task2 done"
|
|
576
594
|
* // timestamp=... level=INFO fiber=#2 message="task1 done"
|
|
@@ -598,7 +616,8 @@ export const allWith = fiberRuntime.allWith;
|
|
|
598
616
|
* These options provide flexibility in running the effects concurrently or
|
|
599
617
|
* adjusting other execution details.
|
|
600
618
|
*
|
|
601
|
-
*
|
|
619
|
+
* **Example**
|
|
620
|
+
*
|
|
602
621
|
* ```ts
|
|
603
622
|
* import { Effect } from "effect"
|
|
604
623
|
*
|
|
@@ -614,7 +633,7 @@ export const allWith = fiberRuntime.allWith;
|
|
|
614
633
|
* console.log(successfulResults)
|
|
615
634
|
* })
|
|
616
635
|
*
|
|
617
|
-
*
|
|
636
|
+
* Effect.runFork(program)
|
|
618
637
|
* // Output: [1, 2]
|
|
619
638
|
*
|
|
620
639
|
* ```
|
|
@@ -646,10 +665,8 @@ export const allSuccesses = fiberRuntime.allSuccesses;
|
|
|
646
665
|
* This function allows you to conditionally skip over a part of the collection
|
|
647
666
|
* based on some criteria defined in the predicate.
|
|
648
667
|
*
|
|
649
|
-
*
|
|
650
|
-
* predicate returns `true`.
|
|
668
|
+
* **Example**
|
|
651
669
|
*
|
|
652
|
-
* @example
|
|
653
670
|
* ```ts
|
|
654
671
|
* import { Effect } from "effect"
|
|
655
672
|
*
|
|
@@ -661,10 +678,13 @@ export const allSuccesses = fiberRuntime.allSuccesses;
|
|
|
661
678
|
* console.log(result)
|
|
662
679
|
* })
|
|
663
680
|
*
|
|
664
|
-
*
|
|
681
|
+
* Effect.runFork(program)
|
|
665
682
|
* // Output: [5, 6]
|
|
666
683
|
* ```
|
|
667
684
|
*
|
|
685
|
+
* @see {@link dropWhile} for a similar function that drops elements while the
|
|
686
|
+
* predicate returns `true`.
|
|
687
|
+
*
|
|
668
688
|
* @since 2.0.0
|
|
669
689
|
* @category Collecting
|
|
670
690
|
*/
|
|
@@ -691,10 +711,8 @@ export const dropUntil = effect.dropUntil;
|
|
|
691
711
|
* based on a condition, and only keep the rest when the condition no longer
|
|
692
712
|
* holds.
|
|
693
713
|
*
|
|
694
|
-
*
|
|
695
|
-
* predicate returns `true`.
|
|
714
|
+
* **Example**
|
|
696
715
|
*
|
|
697
|
-
* @example
|
|
698
716
|
* ```ts
|
|
699
717
|
* import { Effect } from "effect"
|
|
700
718
|
*
|
|
@@ -706,10 +724,13 @@ export const dropUntil = effect.dropUntil;
|
|
|
706
724
|
* console.log(result)
|
|
707
725
|
* })
|
|
708
726
|
*
|
|
709
|
-
*
|
|
727
|
+
* Effect.runFork(program)
|
|
710
728
|
* // Output: [4, 5, 6]
|
|
711
729
|
* ```
|
|
712
730
|
*
|
|
731
|
+
* @see {@link dropUntil} for a similar function that drops elements until the
|
|
732
|
+
* predicate returns `true`.
|
|
733
|
+
*
|
|
713
734
|
* @since 2.0.0
|
|
714
735
|
* @category Collecting
|
|
715
736
|
*/
|
|
@@ -740,10 +761,8 @@ export const dropWhile = effect.dropWhile;
|
|
|
740
761
|
* numbers from a list until a certain threshold is reached, or gather items
|
|
741
762
|
* until a specific condition is met.
|
|
742
763
|
*
|
|
743
|
-
*
|
|
744
|
-
* predicate returns `true`.
|
|
764
|
+
* **Example**
|
|
745
765
|
*
|
|
746
|
-
* @example
|
|
747
766
|
* ```ts
|
|
748
767
|
* import { Effect } from "effect"
|
|
749
768
|
*
|
|
@@ -755,10 +774,13 @@ export const dropWhile = effect.dropWhile;
|
|
|
755
774
|
* console.log(result)
|
|
756
775
|
* })
|
|
757
776
|
*
|
|
758
|
-
*
|
|
777
|
+
* Effect.runFork(program)
|
|
759
778
|
* // Output: [ 1, 2, 3, 4 ]
|
|
760
779
|
* ```
|
|
761
780
|
*
|
|
781
|
+
* @see {@link takeWhile} for a similar function that takes elements while the
|
|
782
|
+
* predicate returns `true`.
|
|
783
|
+
*
|
|
762
784
|
* @since 2.0.0
|
|
763
785
|
* @category Collecting
|
|
764
786
|
*/
|
|
@@ -779,9 +801,8 @@ export const takeUntil = effect.takeUntil;
|
|
|
779
801
|
*
|
|
780
802
|
* Once the predicate returns `false`, the remaining elements are discarded.
|
|
781
803
|
*
|
|
782
|
-
*
|
|
804
|
+
* **Example**
|
|
783
805
|
*
|
|
784
|
-
* @example
|
|
785
806
|
* ```ts
|
|
786
807
|
* import { Effect } from "effect"
|
|
787
808
|
*
|
|
@@ -793,10 +814,12 @@ export const takeUntil = effect.takeUntil;
|
|
|
793
814
|
* console.log(result)
|
|
794
815
|
* })
|
|
795
816
|
*
|
|
796
|
-
*
|
|
817
|
+
* Effect.runFork(program)
|
|
797
818
|
* // Output: [1, 2, 3]
|
|
798
819
|
* ```
|
|
799
820
|
*
|
|
821
|
+
* @see {@link takeUntil} for a similar function that takes elements until the predicate returns `true`.
|
|
822
|
+
*
|
|
800
823
|
* @since 2.0.0
|
|
801
824
|
* @category Collecting
|
|
802
825
|
*/
|
|
@@ -822,10 +845,8 @@ export const takeWhile = effect.takeWhile;
|
|
|
822
845
|
* collection meet certain criteria, even when the evaluation of each item
|
|
823
846
|
* involves effects, such as asynchronous checks or complex computations.
|
|
824
847
|
*
|
|
825
|
-
*
|
|
826
|
-
* whether **any** element satisfies the predicate.
|
|
848
|
+
* **Example**
|
|
827
849
|
*
|
|
828
|
-
* @example
|
|
829
850
|
* ```ts
|
|
830
851
|
* import { Effect } from "effect"
|
|
831
852
|
*
|
|
@@ -837,10 +858,13 @@ export const takeWhile = effect.takeWhile;
|
|
|
837
858
|
* console.log(allEven)
|
|
838
859
|
* })
|
|
839
860
|
*
|
|
840
|
-
*
|
|
861
|
+
* Effect.runFork(program)
|
|
841
862
|
* // Output: true
|
|
842
863
|
* ```
|
|
843
864
|
*
|
|
865
|
+
* @see {@link exists} for a similar function that returns a boolean indicating
|
|
866
|
+
* whether **any** element satisfies the predicate.
|
|
867
|
+
*
|
|
844
868
|
* @since 2.0.0
|
|
845
869
|
* @category Condition Checking
|
|
846
870
|
*/
|
|
@@ -867,10 +891,8 @@ export const every = effect.every;
|
|
|
867
891
|
* This function allows you to quickly check for a condition in a collection
|
|
868
892
|
* without having to manually iterate over it.
|
|
869
893
|
*
|
|
870
|
-
*
|
|
871
|
-
* satisfy the predicate.
|
|
894
|
+
* **Example**
|
|
872
895
|
*
|
|
873
|
-
* @example
|
|
874
896
|
* ```ts
|
|
875
897
|
* import { Effect } from "effect"
|
|
876
898
|
*
|
|
@@ -882,10 +904,13 @@ export const every = effect.every;
|
|
|
882
904
|
* console.log(hasLargeNumber)
|
|
883
905
|
* })
|
|
884
906
|
*
|
|
885
|
-
*
|
|
907
|
+
* Effect.runFork(program)
|
|
886
908
|
* // Output: true
|
|
887
909
|
* ```
|
|
888
910
|
*
|
|
911
|
+
* @see {@link every} for a similar function that checks if **all** elements
|
|
912
|
+
* satisfy the predicate.
|
|
913
|
+
*
|
|
889
914
|
* @since 2.0.0
|
|
890
915
|
* @category Condition Checking
|
|
891
916
|
*/
|
|
@@ -914,7 +939,8 @@ export const exists = fiberRuntime.exists;
|
|
|
914
939
|
* This function allows you to selectively keep or remove elements based on a
|
|
915
940
|
* condition that may involve asynchronous or side-effect-causing operations.
|
|
916
941
|
*
|
|
917
|
-
*
|
|
942
|
+
* **Example**
|
|
943
|
+
*
|
|
918
944
|
* ```ts
|
|
919
945
|
* import { Effect } from "effect"
|
|
920
946
|
*
|
|
@@ -926,7 +952,7 @@ export const exists = fiberRuntime.exists;
|
|
|
926
952
|
* console.log(result)
|
|
927
953
|
* })
|
|
928
954
|
*
|
|
929
|
-
*
|
|
955
|
+
* Effect.runFork(program)
|
|
930
956
|
* // Output: [2, 4]
|
|
931
957
|
* ```
|
|
932
958
|
*
|
|
@@ -942,7 +968,8 @@ export const filter = fiberRuntime.filter;
|
|
|
942
968
|
* element is kept; if it returns `None`, the element is removed. The operation
|
|
943
969
|
* is done sequentially for each element.
|
|
944
970
|
*
|
|
945
|
-
*
|
|
971
|
+
* **Example**
|
|
972
|
+
*
|
|
946
973
|
* ```ts
|
|
947
974
|
* import { Console, Effect, Option } from "effect"
|
|
948
975
|
*
|
|
@@ -957,7 +984,7 @@ export const filter = fiberRuntime.filter;
|
|
|
957
984
|
* (n) => n % 2 === 0 ? Option.some(n) : Option.none()
|
|
958
985
|
* )
|
|
959
986
|
*
|
|
960
|
-
*
|
|
987
|
+
* Effect.runPromise(program).then(console.log)
|
|
961
988
|
* // Output:
|
|
962
989
|
* // task1 done
|
|
963
990
|
* // task2 done
|
|
@@ -992,7 +1019,8 @@ export const filterMap = effect.filterMap;
|
|
|
992
1019
|
* condition, even when the evaluation involves effects like asynchronous
|
|
993
1020
|
* operations or side effects.
|
|
994
1021
|
*
|
|
995
|
-
*
|
|
1022
|
+
* **Example**
|
|
1023
|
+
*
|
|
996
1024
|
* ```ts
|
|
997
1025
|
* import { Effect } from "effect"
|
|
998
1026
|
*
|
|
@@ -1004,7 +1032,7 @@ export const filterMap = effect.filterMap;
|
|
|
1004
1032
|
* console.log(result)
|
|
1005
1033
|
* })
|
|
1006
1034
|
*
|
|
1007
|
-
*
|
|
1035
|
+
* Effect.runFork(program)
|
|
1008
1036
|
* // Output: { _id: 'Option', _tag: 'Some', value: 4 }
|
|
1009
1037
|
* ```
|
|
1010
1038
|
*
|
|
@@ -1033,18 +1061,16 @@ export const findFirst = effect.findFirst;
|
|
|
1033
1061
|
* If the `discard` option is set to `true`, the intermediate results are not
|
|
1034
1062
|
* collected, and the final result of the operation is `void`.
|
|
1035
1063
|
*
|
|
1036
|
-
*
|
|
1064
|
+
* **Example** (Applying Effects to Iterable Elements)
|
|
1037
1065
|
*
|
|
1038
|
-
* @example
|
|
1039
1066
|
* ```ts
|
|
1040
|
-
* // Title: Applying Effects to Iterable Elements
|
|
1041
1067
|
* import { Effect, Console } from "effect"
|
|
1042
1068
|
*
|
|
1043
1069
|
* const result = Effect.forEach([1, 2, 3, 4, 5], (n, index) =>
|
|
1044
1070
|
* Console.log(`Currently at index ${index}`).pipe(Effect.as(n * 2))
|
|
1045
1071
|
* )
|
|
1046
1072
|
*
|
|
1047
|
-
*
|
|
1073
|
+
* Effect.runPromise(result).then(console.log)
|
|
1048
1074
|
* // Output:
|
|
1049
1075
|
* // Currently at index 0
|
|
1050
1076
|
* // Currently at index 1
|
|
@@ -1054,8 +1080,9 @@ export const findFirst = effect.findFirst;
|
|
|
1054
1080
|
* // [ 2, 4, 6, 8, 10 ]
|
|
1055
1081
|
* ```
|
|
1056
1082
|
*
|
|
1057
|
-
*
|
|
1058
|
-
*
|
|
1083
|
+
* **Example** (Discarding Results)
|
|
1084
|
+
*
|
|
1085
|
+
* ```ts
|
|
1059
1086
|
* import { Effect, Console } from "effect"
|
|
1060
1087
|
*
|
|
1061
1088
|
* // Apply effects but discard the results
|
|
@@ -1066,7 +1093,7 @@ export const findFirst = effect.findFirst;
|
|
|
1066
1093
|
* { discard: true }
|
|
1067
1094
|
* )
|
|
1068
1095
|
*
|
|
1069
|
-
*
|
|
1096
|
+
* Effect.runPromise(result).then(console.log)
|
|
1070
1097
|
* // Output:
|
|
1071
1098
|
* // Currently at index 0
|
|
1072
1099
|
* // Currently at index 1
|
|
@@ -1074,6 +1101,9 @@ export const findFirst = effect.findFirst;
|
|
|
1074
1101
|
* // Currently at index 3
|
|
1075
1102
|
* // Currently at index 4
|
|
1076
1103
|
* // undefined
|
|
1104
|
+
* ```
|
|
1105
|
+
*
|
|
1106
|
+
* @see {@link all} for combining multiple effects into one.
|
|
1077
1107
|
*
|
|
1078
1108
|
* @since 2.0.0
|
|
1079
1109
|
* @category Looping
|
|
@@ -1089,7 +1119,8 @@ export const forEach = fiberRuntime.forEach;
|
|
|
1089
1119
|
* collection and want to handle the case where the collection might be empty
|
|
1090
1120
|
* without causing an unhandled exception.
|
|
1091
1121
|
*
|
|
1092
|
-
*
|
|
1122
|
+
* **Example**
|
|
1123
|
+
*
|
|
1093
1124
|
* ```ts
|
|
1094
1125
|
* import { Effect } from "effect"
|
|
1095
1126
|
*
|
|
@@ -1101,7 +1132,7 @@ export const forEach = fiberRuntime.forEach;
|
|
|
1101
1132
|
* console.log(firstElement)
|
|
1102
1133
|
* })
|
|
1103
1134
|
*
|
|
1104
|
-
*
|
|
1135
|
+
* Effect.runFork(program)
|
|
1105
1136
|
* // Output: 1
|
|
1106
1137
|
* ```
|
|
1107
1138
|
*
|
|
@@ -1129,7 +1160,8 @@ export const head = effect.head;
|
|
|
1129
1160
|
* These options provide flexibility in running the effects concurrently or
|
|
1130
1161
|
* adjusting other execution details.
|
|
1131
1162
|
*
|
|
1132
|
-
*
|
|
1163
|
+
* **Example**
|
|
1164
|
+
*
|
|
1133
1165
|
* ```ts
|
|
1134
1166
|
* import { Effect } from "effect"
|
|
1135
1167
|
*
|
|
@@ -1142,7 +1174,7 @@ export const head = effect.head;
|
|
|
1142
1174
|
* console.log(total)
|
|
1143
1175
|
* })
|
|
1144
1176
|
*
|
|
1145
|
-
*
|
|
1177
|
+
* Effect.runFork(program)
|
|
1146
1178
|
* // Output: 6
|
|
1147
1179
|
* ```
|
|
1148
1180
|
*
|
|
@@ -1174,10 +1206,8 @@ export const mergeAll = fiberRuntime.mergeAll;
|
|
|
1174
1206
|
* work with valid data. The function ensures that failures are captured, while
|
|
1175
1207
|
* successes are processed normally.
|
|
1176
1208
|
*
|
|
1177
|
-
*
|
|
1178
|
-
* @see {@link validateFirst} for a function that stops at the first success.
|
|
1209
|
+
* **Example**
|
|
1179
1210
|
*
|
|
1180
|
-
* @example
|
|
1181
1211
|
* ```ts
|
|
1182
1212
|
* import { Effect } from "effect"
|
|
1183
1213
|
*
|
|
@@ -1191,11 +1221,14 @@ export const mergeAll = fiberRuntime.mergeAll;
|
|
|
1191
1221
|
* }
|
|
1192
1222
|
* })
|
|
1193
1223
|
*
|
|
1194
|
-
*
|
|
1224
|
+
* Effect.runPromise(program).then(console.log, console.error)
|
|
1195
1225
|
* // Output:
|
|
1196
1226
|
* // [ [ '1 is not even', '3 is not even' ], [ 0, 2, 4 ] ]
|
|
1197
1227
|
* ```
|
|
1198
1228
|
*
|
|
1229
|
+
* @see {@link validateAll} for a function that either collects all failures or all successes.
|
|
1230
|
+
* @see {@link validateFirst} for a function that stops at the first success.
|
|
1231
|
+
*
|
|
1199
1232
|
* @since 2.0.0
|
|
1200
1233
|
* @category Error Accumulation
|
|
1201
1234
|
*/
|
|
@@ -1219,10 +1252,8 @@ export const partition = fiberRuntime.partition;
|
|
|
1219
1252
|
* numbers or combining results from multiple tasks. It ensures that operations
|
|
1220
1253
|
* are performed one after the other, maintaining the order of the elements.
|
|
1221
1254
|
*
|
|
1222
|
-
*
|
|
1223
|
-
* @see {@link reduceRight} for a similar function that works from right to left.
|
|
1255
|
+
* **Example**
|
|
1224
1256
|
*
|
|
1225
|
-
* @example
|
|
1226
1257
|
* ```ts
|
|
1227
1258
|
* import { Console, Effect } from "effect"
|
|
1228
1259
|
*
|
|
@@ -1238,7 +1269,7 @@ export const partition = fiberRuntime.partition;
|
|
|
1238
1269
|
* .pipe(Effect.map((order) => acc + order.price))
|
|
1239
1270
|
* )
|
|
1240
1271
|
*
|
|
1241
|
-
*
|
|
1272
|
+
* Effect.runPromise(program).then(console.log)
|
|
1242
1273
|
* // Output:
|
|
1243
1274
|
* // Order 1 processed
|
|
1244
1275
|
* // Order 2 processed
|
|
@@ -1247,6 +1278,9 @@ export const partition = fiberRuntime.partition;
|
|
|
1247
1278
|
* // 1000
|
|
1248
1279
|
* ```
|
|
1249
1280
|
*
|
|
1281
|
+
* @see {@link reduceWhile} for a similar function that stops the process based on a predicate.
|
|
1282
|
+
* @see {@link reduceRight} for a similar function that works from right to left.
|
|
1283
|
+
*
|
|
1250
1284
|
* @since 2.0.0
|
|
1251
1285
|
* @category Collecting
|
|
1252
1286
|
*/
|
|
@@ -1271,7 +1305,8 @@ export const reduce = effect.reduce;
|
|
|
1271
1305
|
* if you want to sum values in a list but stop as soon as the sum exceeds a
|
|
1272
1306
|
* certain threshold, you can use this function.
|
|
1273
1307
|
*
|
|
1274
|
-
*
|
|
1308
|
+
* **Example**
|
|
1309
|
+
*
|
|
1275
1310
|
* ```ts
|
|
1276
1311
|
* import { Console, Effect } from "effect"
|
|
1277
1312
|
*
|
|
@@ -1290,7 +1325,7 @@ export const reduce = effect.reduce;
|
|
|
1290
1325
|
* }
|
|
1291
1326
|
* )
|
|
1292
1327
|
*
|
|
1293
|
-
*
|
|
1328
|
+
* Effect.runPromise(program).then(console.log)
|
|
1294
1329
|
* // Output:
|
|
1295
1330
|
* // Order 1 processed
|
|
1296
1331
|
* // Order 2 processed
|
|
@@ -1321,9 +1356,8 @@ export const reduceWhile = effect.reduceWhile;
|
|
|
1321
1356
|
* numbers or combining results from multiple tasks. It ensures that operations
|
|
1322
1357
|
* are performed one after the other, maintaining the order of the elements.
|
|
1323
1358
|
*
|
|
1324
|
-
*
|
|
1359
|
+
* **Example**
|
|
1325
1360
|
*
|
|
1326
|
-
* @example
|
|
1327
1361
|
* ```ts
|
|
1328
1362
|
* import { Console, Effect } from "effect"
|
|
1329
1363
|
*
|
|
@@ -1339,7 +1373,7 @@ export const reduceWhile = effect.reduceWhile;
|
|
|
1339
1373
|
* .pipe(Effect.map((order) => acc + order.price))
|
|
1340
1374
|
* )
|
|
1341
1375
|
*
|
|
1342
|
-
*
|
|
1376
|
+
* Effect.runPromise(program).then(console.log)
|
|
1343
1377
|
* // Output:
|
|
1344
1378
|
* // Order 4 processed
|
|
1345
1379
|
* // Order 3 processed
|
|
@@ -1348,6 +1382,8 @@ export const reduceWhile = effect.reduceWhile;
|
|
|
1348
1382
|
* // 1000
|
|
1349
1383
|
* ```
|
|
1350
1384
|
*
|
|
1385
|
+
* @see {@link reduce} for a similar function that works from left to right.
|
|
1386
|
+
*
|
|
1351
1387
|
* @since 2.0.0
|
|
1352
1388
|
* @category Collecting
|
|
1353
1389
|
*/
|
|
@@ -1368,7 +1404,8 @@ export const reduceRight = effect.reduceRight;
|
|
|
1368
1404
|
* These options provide flexibility in running the effects concurrently or
|
|
1369
1405
|
* adjusting other execution details.
|
|
1370
1406
|
*
|
|
1371
|
-
*
|
|
1407
|
+
* **Example**
|
|
1408
|
+
*
|
|
1372
1409
|
* ```ts
|
|
1373
1410
|
* import { Console, Effect } from "effect"
|
|
1374
1411
|
*
|
|
@@ -1382,7 +1419,7 @@ export const reduceRight = effect.reduceRight;
|
|
|
1382
1419
|
* (acc, order, i) => acc + order.price
|
|
1383
1420
|
* )
|
|
1384
1421
|
*
|
|
1385
|
-
*
|
|
1422
|
+
* Effect.runPromise(program).then(console.log)
|
|
1386
1423
|
* // Output:
|
|
1387
1424
|
* // Order 1 processed
|
|
1388
1425
|
* // Order 2 processed
|
|
@@ -1404,7 +1441,8 @@ export const reduceEffect = fiberRuntime.reduceEffect;
|
|
|
1404
1441
|
* (`n`). The result is an array of `n` effects, each of which is identical to
|
|
1405
1442
|
* the original effect.
|
|
1406
1443
|
*
|
|
1407
|
-
*
|
|
1444
|
+
* **Example**
|
|
1445
|
+
*
|
|
1408
1446
|
* ```ts
|
|
1409
1447
|
* import { Console, Effect } from "effect"
|
|
1410
1448
|
*
|
|
@@ -1421,7 +1459,7 @@ export const reduceEffect = fiberRuntime.reduceEffect;
|
|
|
1421
1459
|
* }
|
|
1422
1460
|
* })
|
|
1423
1461
|
*
|
|
1424
|
-
*
|
|
1462
|
+
* Effect.runFork(program)
|
|
1425
1463
|
* // Output:
|
|
1426
1464
|
* // Hello, World!
|
|
1427
1465
|
* // Hello, World!
|
|
@@ -1451,7 +1489,8 @@ export const replicate = fiberRuntime.replicate;
|
|
|
1451
1489
|
* These options provide flexibility in running the effects concurrently or
|
|
1452
1490
|
* adjusting other execution details.
|
|
1453
1491
|
*
|
|
1454
|
-
*
|
|
1492
|
+
* **Example**
|
|
1493
|
+
*
|
|
1455
1494
|
* ```ts
|
|
1456
1495
|
* import { Console, Effect } from "effect"
|
|
1457
1496
|
*
|
|
@@ -1467,7 +1506,7 @@ export const replicate = fiberRuntime.replicate;
|
|
|
1467
1506
|
* yield* Console.log(`Results: ${results.join(", ")}`)
|
|
1468
1507
|
* })
|
|
1469
1508
|
*
|
|
1470
|
-
*
|
|
1509
|
+
* Effect.runFork(program)
|
|
1471
1510
|
* // Output:
|
|
1472
1511
|
* // Task completed
|
|
1473
1512
|
* // Task completed
|
|
@@ -1501,11 +1540,8 @@ export const replicateEffect = fiberRuntime.replicateEffect;
|
|
|
1501
1540
|
* so this function is not suitable when you need to keep the successful results
|
|
1502
1541
|
* in case of errors.
|
|
1503
1542
|
*
|
|
1504
|
-
*
|
|
1505
|
-
* @see {@link partition} when you need to separate successes and failures
|
|
1506
|
-
* instead of losing successes with errors.
|
|
1543
|
+
* **Example**
|
|
1507
1544
|
*
|
|
1508
|
-
* @example
|
|
1509
1545
|
* ```ts
|
|
1510
1546
|
* import { Effect, Console } from "effect"
|
|
1511
1547
|
*
|
|
@@ -1519,7 +1555,7 @@ export const replicateEffect = fiberRuntime.replicateEffect;
|
|
|
1519
1555
|
* }
|
|
1520
1556
|
* })
|
|
1521
1557
|
*
|
|
1522
|
-
*
|
|
1558
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
1523
1559
|
* // Output:
|
|
1524
1560
|
* // item 1
|
|
1525
1561
|
* // item 2
|
|
@@ -1535,6 +1571,10 @@ export const replicateEffect = fiberRuntime.replicateEffect;
|
|
|
1535
1571
|
* // }
|
|
1536
1572
|
* ```
|
|
1537
1573
|
*
|
|
1574
|
+
* @see {@link forEach} for a similar function that stops at the first error.
|
|
1575
|
+
* @see {@link partition} when you need to separate successes and failures
|
|
1576
|
+
* instead of losing successes with errors.
|
|
1577
|
+
*
|
|
1538
1578
|
* @since 2.0.0
|
|
1539
1579
|
* @category Error Accumulation
|
|
1540
1580
|
*/
|
|
@@ -1553,11 +1593,8 @@ export const validateAll = fiberRuntime.validateAll;
|
|
|
1553
1593
|
* errors. This can be useful when you are interested in the first successful
|
|
1554
1594
|
* result and want to avoid processing further once a valid result is found.
|
|
1555
1595
|
*
|
|
1556
|
-
*
|
|
1557
|
-
* @see {@link firstSuccessOf} for a similar function that processes multiple
|
|
1558
|
-
* effects and returns the first successful one or the last error.
|
|
1596
|
+
* **Example**
|
|
1559
1597
|
*
|
|
1560
|
-
* @example
|
|
1561
1598
|
* ```ts
|
|
1562
1599
|
* import { Effect, Console } from "effect"
|
|
1563
1600
|
*
|
|
@@ -1571,12 +1608,16 @@ export const validateAll = fiberRuntime.validateAll;
|
|
|
1571
1608
|
* }
|
|
1572
1609
|
* })
|
|
1573
1610
|
*
|
|
1574
|
-
*
|
|
1611
|
+
* Effect.runPromise(program).then(console.log, console.error)
|
|
1575
1612
|
* // Output:
|
|
1576
1613
|
* // item 4
|
|
1577
1614
|
* // 4
|
|
1578
1615
|
* ```
|
|
1579
1616
|
*
|
|
1617
|
+
* @see {@link validateAll} for a similar function that accumulates all results.
|
|
1618
|
+
* @see {@link firstSuccessOf} for a similar function that processes multiple
|
|
1619
|
+
* effects and returns the first successful one or the last error.
|
|
1620
|
+
*
|
|
1580
1621
|
* @since 2.0.0
|
|
1581
1622
|
* @category Error Accumulation
|
|
1582
1623
|
*/
|
|
@@ -1605,9 +1646,9 @@ export const validateFirst = fiberRuntime.validateFirst;
|
|
|
1605
1646
|
* Use `Effect.async` when dealing with APIs that use callback-style instead of
|
|
1606
1647
|
* `async/await` or `Promise`.
|
|
1607
1648
|
*
|
|
1608
|
-
*
|
|
1649
|
+
* **Example** (Wrapping a Callback API)
|
|
1650
|
+
*
|
|
1609
1651
|
* ```ts
|
|
1610
|
-
* // Title: Wrapping a Callback API
|
|
1611
1652
|
* import { Effect } from "effect"
|
|
1612
1653
|
* import * as NodeFS from "node:fs"
|
|
1613
1654
|
*
|
|
@@ -1629,8 +1670,9 @@ export const validateFirst = fiberRuntime.validateFirst;
|
|
|
1629
1670
|
* const program = readFile("example.txt")
|
|
1630
1671
|
* ```
|
|
1631
1672
|
*
|
|
1632
|
-
*
|
|
1633
|
-
*
|
|
1673
|
+
* **Example** (Handling Interruption with Cleanup)
|
|
1674
|
+
*
|
|
1675
|
+
* ```ts
|
|
1634
1676
|
* import { Effect, Fiber } from "effect"
|
|
1635
1677
|
* import * as NodeFS from "node:fs"
|
|
1636
1678
|
*
|
|
@@ -1665,12 +1707,14 @@ export const validateFirst = fiberRuntime.validateFirst;
|
|
|
1665
1707
|
* })
|
|
1666
1708
|
*
|
|
1667
1709
|
* // Run the program
|
|
1668
|
-
*
|
|
1710
|
+
* Effect.runPromise(program)
|
|
1669
1711
|
* // Output:
|
|
1670
1712
|
* // Cleaning up example.txt
|
|
1713
|
+
* ```
|
|
1714
|
+
*
|
|
1715
|
+
* **Example** (Handling Interruption with AbortSignal)
|
|
1671
1716
|
*
|
|
1672
|
-
*
|
|
1673
|
-
* // Title: Handling Interruption with AbortSignal
|
|
1717
|
+
* ```ts
|
|
1674
1718
|
* import { Effect, Fiber } from "effect"
|
|
1675
1719
|
*
|
|
1676
1720
|
* // A task that supports interruption using AbortSignal
|
|
@@ -1696,9 +1740,10 @@ export const validateFirst = fiberRuntime.validateFirst;
|
|
|
1696
1740
|
* })
|
|
1697
1741
|
*
|
|
1698
1742
|
* // Run the program
|
|
1699
|
-
*
|
|
1743
|
+
* Effect.runPromise(program)
|
|
1700
1744
|
* // Output:
|
|
1701
1745
|
* // Abort signal received
|
|
1746
|
+
* ```
|
|
1702
1747
|
*
|
|
1703
1748
|
* @since 2.0.0
|
|
1704
1749
|
* @category Creating Effects
|
|
@@ -1717,7 +1762,8 @@ export const asyncEffect = runtime_.asyncEffect;
|
|
|
1717
1762
|
* It is meant to be called with a bag of instructions that become available in
|
|
1718
1763
|
* the "this" of the effect.
|
|
1719
1764
|
*
|
|
1720
|
-
*
|
|
1765
|
+
* **Example**
|
|
1766
|
+
*
|
|
1721
1767
|
* ```ts
|
|
1722
1768
|
* import { Effect } from "effect"
|
|
1723
1769
|
*
|
|
@@ -1745,11 +1791,9 @@ export const withFiberRuntime = core.withFiberRuntime;
|
|
|
1745
1791
|
* will keep propagating unless it is handled. You can handle the error with
|
|
1746
1792
|
* functions like {@link catchAll} or {@link catchTag}.
|
|
1747
1793
|
*
|
|
1748
|
-
*
|
|
1794
|
+
* **Example** (Creating a Failed Effect)
|
|
1749
1795
|
*
|
|
1750
|
-
* @example
|
|
1751
1796
|
* ```ts
|
|
1752
|
-
* // Title: Creating a Failed Effect
|
|
1753
1797
|
* import { Effect } from "effect"
|
|
1754
1798
|
*
|
|
1755
1799
|
* // ┌─── Effect<never, Error, never>
|
|
@@ -1759,6 +1803,8 @@ export const withFiberRuntime = core.withFiberRuntime;
|
|
|
1759
1803
|
* )
|
|
1760
1804
|
* ```
|
|
1761
1805
|
*
|
|
1806
|
+
* @see {@link succeed} to create an effect that represents a successful value.
|
|
1807
|
+
*
|
|
1762
1808
|
* @since 2.0.0
|
|
1763
1809
|
* @category Creating Effects
|
|
1764
1810
|
*/
|
|
@@ -1802,14 +1848,9 @@ export const failCauseSync = core.failCauseSync;
|
|
|
1802
1848
|
* should not be handled as regular errors but instead represent unrecoverable
|
|
1803
1849
|
* defects.
|
|
1804
1850
|
*
|
|
1805
|
-
*
|
|
1806
|
-
* lazily.
|
|
1807
|
-
* @see {@link dieMessage} for a variant that throws a `RuntimeException` with a
|
|
1808
|
-
* message.
|
|
1851
|
+
* **Example** (Terminating on Division by Zero with a Specified Error)
|
|
1809
1852
|
*
|
|
1810
|
-
* @example
|
|
1811
1853
|
* ```ts
|
|
1812
|
-
* // Title: Terminating on Division by Zero with a Specified Error
|
|
1813
1854
|
* import { Effect } from "effect"
|
|
1814
1855
|
*
|
|
1815
1856
|
* const divide = (a: number, b: number) =>
|
|
@@ -1821,12 +1862,17 @@ export const failCauseSync = core.failCauseSync;
|
|
|
1821
1862
|
* // ▼
|
|
1822
1863
|
* const program = divide(1, 0)
|
|
1823
1864
|
*
|
|
1824
|
-
*
|
|
1865
|
+
* Effect.runPromise(program).catch(console.error)
|
|
1825
1866
|
* // Output:
|
|
1826
1867
|
* // (FiberFailure) Error: Cannot divide by zero
|
|
1827
1868
|
* // ...stack trace...
|
|
1828
1869
|
* ```
|
|
1829
1870
|
*
|
|
1871
|
+
* @see {@link dieSync} for a variant that throws a specified error, evaluated
|
|
1872
|
+
* lazily.
|
|
1873
|
+
* @see {@link dieMessage} for a variant that throws a `RuntimeException` with a
|
|
1874
|
+
* message.
|
|
1875
|
+
*
|
|
1830
1876
|
* @since 2.0.0
|
|
1831
1877
|
* @category Creating Effects
|
|
1832
1878
|
*/
|
|
@@ -1849,13 +1895,9 @@ export const die = core.die;
|
|
|
1849
1895
|
* Use this function when you want to terminate a fiber due to an unrecoverable
|
|
1850
1896
|
* defect and include a clear explanation in the message.
|
|
1851
1897
|
*
|
|
1852
|
-
*
|
|
1853
|
-
* @see {@link dieSync} for a variant that throws a specified error, evaluated
|
|
1854
|
-
* lazily.
|
|
1898
|
+
* **Example** (Terminating on Division by Zero with a Specified Message)
|
|
1855
1899
|
*
|
|
1856
|
-
* @example
|
|
1857
1900
|
* ```ts
|
|
1858
|
-
* // Title: Terminating on Division by Zero with a Specified Message
|
|
1859
1901
|
* import { Effect } from "effect"
|
|
1860
1902
|
*
|
|
1861
1903
|
* const divide = (a: number, b: number) =>
|
|
@@ -1867,12 +1909,16 @@ export const die = core.die;
|
|
|
1867
1909
|
* // ▼
|
|
1868
1910
|
* const program = divide(1, 0)
|
|
1869
1911
|
*
|
|
1870
|
-
*
|
|
1912
|
+
* Effect.runPromise(program).catch(console.error)
|
|
1871
1913
|
* // Output:
|
|
1872
1914
|
* // (FiberFailure) RuntimeException: Cannot divide by zero
|
|
1873
1915
|
* // ...stack trace...
|
|
1874
1916
|
* ```
|
|
1875
1917
|
*
|
|
1918
|
+
* @see {@link die} for a variant that throws a specified error.
|
|
1919
|
+
* @see {@link dieSync} for a variant that throws a specified error, evaluated
|
|
1920
|
+
* lazily.
|
|
1921
|
+
*
|
|
1876
1922
|
* @since 2.0.0
|
|
1877
1923
|
* @category Creating Effects
|
|
1878
1924
|
*/
|
|
@@ -1906,7 +1952,8 @@ export const dieSync = core.dieSync;
|
|
|
1906
1952
|
* explicit control over the execution of effects. You can `yield*` values from
|
|
1907
1953
|
* effects and return the final result at the end.
|
|
1908
1954
|
*
|
|
1909
|
-
*
|
|
1955
|
+
* **Example**
|
|
1956
|
+
*
|
|
1910
1957
|
* ```ts
|
|
1911
1958
|
* import { Effect } from "effect"
|
|
1912
1959
|
*
|
|
@@ -1998,11 +2045,9 @@ export const none = effect.none;
|
|
|
1998
2045
|
*
|
|
1999
2046
|
* Use this function when you are sure the operation will not reject.
|
|
2000
2047
|
*
|
|
2001
|
-
*
|
|
2048
|
+
* **Example** (Delayed Message)
|
|
2002
2049
|
*
|
|
2003
|
-
* @example
|
|
2004
2050
|
* ```ts
|
|
2005
|
-
* // Title: Delayed Message
|
|
2006
2051
|
* import { Effect } from "effect"
|
|
2007
2052
|
*
|
|
2008
2053
|
* const delay = (message: string) =>
|
|
@@ -2020,6 +2065,8 @@ export const none = effect.none;
|
|
|
2020
2065
|
* const program = delay("Async operation completed successfully!")
|
|
2021
2066
|
* ```
|
|
2022
2067
|
*
|
|
2068
|
+
* @see {@link tryPromise} for a version that can handle failures.
|
|
2069
|
+
*
|
|
2023
2070
|
* @since 2.0.0
|
|
2024
2071
|
* @category Creating Effects
|
|
2025
2072
|
*/
|
|
@@ -2032,11 +2079,9 @@ export const promise = effect.promise;
|
|
|
2032
2079
|
* Use this function when you need an effect that completes successfully with a
|
|
2033
2080
|
* specific value without any errors or external dependencies.
|
|
2034
2081
|
*
|
|
2035
|
-
*
|
|
2082
|
+
* **Example** (Creating a Successful Effect)
|
|
2036
2083
|
*
|
|
2037
|
-
* @example
|
|
2038
2084
|
* ```ts
|
|
2039
|
-
* // Title: Creating a Successful Effect
|
|
2040
2085
|
* import { Effect } from "effect"
|
|
2041
2086
|
*
|
|
2042
2087
|
* // Creating an effect that represents a successful scenario
|
|
@@ -2046,6 +2091,8 @@ export const promise = effect.promise;
|
|
|
2046
2091
|
* const success = Effect.succeed(42)
|
|
2047
2092
|
* ```
|
|
2048
2093
|
*
|
|
2094
|
+
* @see {@link fail} to create an effect that represents a failure.
|
|
2095
|
+
*
|
|
2049
2096
|
* @since 2.0.0
|
|
2050
2097
|
* @category Creating Effects
|
|
2051
2098
|
*/
|
|
@@ -2101,9 +2148,9 @@ export const succeedSome = effect.succeedSome;
|
|
|
2101
2148
|
* computations, managing circular dependencies, or resolving type inference
|
|
2102
2149
|
* issues.
|
|
2103
2150
|
*
|
|
2104
|
-
*
|
|
2151
|
+
* **Example** (Lazy Evaluation with Side Effects)
|
|
2152
|
+
*
|
|
2105
2153
|
* ```ts
|
|
2106
|
-
* // Title: Lazy Evaluation with Side Effects
|
|
2107
2154
|
* import { Effect } from "effect"
|
|
2108
2155
|
*
|
|
2109
2156
|
* let i = 0
|
|
@@ -2119,8 +2166,9 @@ export const succeedSome = effect.succeedSome;
|
|
|
2119
2166
|
* console.log(Effect.runSync(good)) // Output: 2
|
|
2120
2167
|
* ```
|
|
2121
2168
|
*
|
|
2122
|
-
*
|
|
2123
|
-
*
|
|
2169
|
+
* **Example** (Recursive Fibonacci)
|
|
2170
|
+
*
|
|
2171
|
+
* ```ts
|
|
2124
2172
|
* import { Effect } from "effect"
|
|
2125
2173
|
*
|
|
2126
2174
|
* const blowsUp = (n: number): Effect.Effect<number> =>
|
|
@@ -2128,7 +2176,7 @@ export const succeedSome = effect.succeedSome;
|
|
|
2128
2176
|
* ? Effect.succeed(1)
|
|
2129
2177
|
* : Effect.zipWith(blowsUp(n - 1), blowsUp(n - 2), (a, b) => a + b)
|
|
2130
2178
|
*
|
|
2131
|
-
*
|
|
2179
|
+
* console.log(Effect.runSync(blowsUp(32)))
|
|
2132
2180
|
* // crash: JavaScript heap out of memory
|
|
2133
2181
|
*
|
|
2134
2182
|
* const allGood = (n: number): Effect.Effect<number> =>
|
|
@@ -2142,9 +2190,11 @@ export const succeedSome = effect.succeedSome;
|
|
|
2142
2190
|
*
|
|
2143
2191
|
* console.log(Effect.runSync(allGood(32)))
|
|
2144
2192
|
* // Output: 3524578
|
|
2193
|
+
* ```
|
|
2145
2194
|
*
|
|
2146
|
-
*
|
|
2147
|
-
*
|
|
2195
|
+
* **Example** (Using Effect.suspend to Help TypeScript Infer Types)
|
|
2196
|
+
*
|
|
2197
|
+
* ```ts
|
|
2148
2198
|
* import { Effect } from "effect"
|
|
2149
2199
|
*
|
|
2150
2200
|
* // Without suspend, TypeScript may struggle with type inference.
|
|
@@ -2165,6 +2215,7 @@ export const succeedSome = effect.succeedSome;
|
|
|
2165
2215
|
* ? Effect.fail(new Error("Cannot divide by zero"))
|
|
2166
2216
|
* : Effect.succeed(a / b)
|
|
2167
2217
|
* )
|
|
2218
|
+
* ```
|
|
2168
2219
|
*
|
|
2169
2220
|
* @since 2.0.0
|
|
2170
2221
|
* @category Creating Effects
|
|
@@ -2187,11 +2238,9 @@ export const suspend = core.suspend;
|
|
|
2187
2238
|
*
|
|
2188
2239
|
* Use this function when you are sure the operation will not fail.
|
|
2189
2240
|
*
|
|
2190
|
-
*
|
|
2241
|
+
* **Example** (Logging a Message)
|
|
2191
2242
|
*
|
|
2192
|
-
* @example
|
|
2193
2243
|
* ```ts
|
|
2194
|
-
* // Title: Logging a Message
|
|
2195
2244
|
* import { Effect } from "effect"
|
|
2196
2245
|
*
|
|
2197
2246
|
* const log = (message: string) =>
|
|
@@ -2204,6 +2253,8 @@ export const suspend = core.suspend;
|
|
|
2204
2253
|
* const program = log("Hello, World!")
|
|
2205
2254
|
* ```
|
|
2206
2255
|
*
|
|
2256
|
+
* @see {@link try_ | try} for a version that can handle failures.
|
|
2257
|
+
*
|
|
2207
2258
|
* @since 2.0.0
|
|
2208
2259
|
* @category Creating Effects
|
|
2209
2260
|
*/
|
|
@@ -2245,9 +2296,8 @@ export {
|
|
|
2245
2296
|
* match the specified type, the function allows the original effect to
|
|
2246
2297
|
* continue as it was.
|
|
2247
2298
|
*
|
|
2248
|
-
*
|
|
2299
|
+
* **Example**
|
|
2249
2300
|
*
|
|
2250
|
-
* @example
|
|
2251
2301
|
* ```ts
|
|
2252
2302
|
* import { Console, Effect } from "effect"
|
|
2253
2303
|
*
|
|
@@ -2269,10 +2319,12 @@ export {
|
|
|
2269
2319
|
* console.log(`Result: ${result}`)
|
|
2270
2320
|
* })
|
|
2271
2321
|
*
|
|
2272
|
-
*
|
|
2322
|
+
* Effect.runFork(program)
|
|
2273
2323
|
* // Output: Result: recovered from error: NetworkError
|
|
2274
2324
|
* ```
|
|
2275
2325
|
*
|
|
2326
|
+
* @see {@link catchTag} for a version that can recover from errors based on a `_tag` discriminator.
|
|
2327
|
+
*
|
|
2276
2328
|
* @since 2.0.0
|
|
2277
2329
|
* @category Error handling
|
|
2278
2330
|
*/
|
|
@@ -2290,12 +2342,9 @@ _catch as catch };
|
|
|
2290
2342
|
* **Note**: This function only handles recoverable errors. It will not recover
|
|
2291
2343
|
* from unrecoverable defects.
|
|
2292
2344
|
*
|
|
2293
|
-
*
|
|
2294
|
-
* recoverable and unrecoverable errors.
|
|
2345
|
+
* **Example** (Providing Recovery Logic for Recoverable Errors)
|
|
2295
2346
|
*
|
|
2296
|
-
* @example
|
|
2297
2347
|
* ```ts
|
|
2298
|
-
* // Title: Providing Recovery Logic for Recoverable Errors
|
|
2299
2348
|
* import { Effect, Random } from "effect"
|
|
2300
2349
|
*
|
|
2301
2350
|
* class HttpError {
|
|
@@ -2329,6 +2378,9 @@ _catch as catch };
|
|
|
2329
2378
|
* )
|
|
2330
2379
|
* ```
|
|
2331
2380
|
*
|
|
2381
|
+
* @see {@link catchAllCause} for a version that can recover from both
|
|
2382
|
+
* recoverable and unrecoverable errors.
|
|
2383
|
+
*
|
|
2332
2384
|
* @since 2.0.0
|
|
2333
2385
|
* @category Error handling
|
|
2334
2386
|
*/
|
|
@@ -2350,9 +2402,9 @@ export const catchAll = core.catchAll;
|
|
|
2350
2402
|
* they often indicate serious issues. However, in some cases, such as
|
|
2351
2403
|
* dynamically loaded plugins, controlled recovery might be needed.
|
|
2352
2404
|
*
|
|
2353
|
-
*
|
|
2405
|
+
* **Example** (Recovering from All Errors)
|
|
2406
|
+
*
|
|
2354
2407
|
* ```ts
|
|
2355
|
-
* // Title: Recovering from All Errors
|
|
2356
2408
|
* import { Cause, Effect } from "effect"
|
|
2357
2409
|
*
|
|
2358
2410
|
* // Define an effect that may fail with a recoverable or unrecoverable error
|
|
@@ -2367,7 +2419,7 @@ export const catchAll = core.catchAll;
|
|
|
2367
2419
|
* )
|
|
2368
2420
|
* )
|
|
2369
2421
|
*
|
|
2370
|
-
*
|
|
2422
|
+
* Effect.runPromise(recovered).then(console.log)
|
|
2371
2423
|
* // Output: "Recovered from a regular error"
|
|
2372
2424
|
* ```
|
|
2373
2425
|
*
|
|
@@ -2398,9 +2450,9 @@ export const catchAllCause = core.catchAllCause;
|
|
|
2398
2450
|
* they often indicate serious issues. However, in some cases, such as
|
|
2399
2451
|
* dynamically loaded plugins, controlled recovery might be needed.
|
|
2400
2452
|
*
|
|
2401
|
-
*
|
|
2453
|
+
* **Example** (Handling All Defects)
|
|
2454
|
+
*
|
|
2402
2455
|
* ```ts
|
|
2403
|
-
* // Title: Handling All Defects
|
|
2404
2456
|
* import { Effect, Cause, Console } from "effect"
|
|
2405
2457
|
*
|
|
2406
2458
|
* // Simulating a runtime error
|
|
@@ -2416,7 +2468,7 @@ export const catchAllCause = core.catchAllCause;
|
|
|
2416
2468
|
* })
|
|
2417
2469
|
*
|
|
2418
2470
|
* // We get an Exit.Success because we caught all defects
|
|
2419
|
-
*
|
|
2471
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
2420
2472
|
* // Output:
|
|
2421
2473
|
* // RuntimeException defect caught: Boom!
|
|
2422
2474
|
* // {
|
|
@@ -2441,9 +2493,9 @@ export const catchAllDefect = effect.catchAllDefect;
|
|
|
2441
2493
|
* alter the error type, so the resulting effect still carries the original
|
|
2442
2494
|
* error type unless a user-defined type guard is used to narrow the type.
|
|
2443
2495
|
*
|
|
2444
|
-
*
|
|
2496
|
+
* **Example** (Catching Specific Errors with a Predicate)
|
|
2497
|
+
*
|
|
2445
2498
|
* ```ts
|
|
2446
|
-
* // Title: Catching Specific Errors with a Predicate
|
|
2447
2499
|
* import { Effect, Random } from "effect"
|
|
2448
2500
|
*
|
|
2449
2501
|
* class HttpError {
|
|
@@ -2495,11 +2547,9 @@ export const catchIf = core.catchIf;
|
|
|
2495
2547
|
* program. This function doesn't alter the error type, meaning the error type
|
|
2496
2548
|
* remains the same as in the original effect.
|
|
2497
2549
|
*
|
|
2498
|
-
*
|
|
2550
|
+
* **Example** (Handling Specific Errors with Effect.catchSome)
|
|
2499
2551
|
*
|
|
2500
|
-
* @example
|
|
2501
2552
|
* ```ts
|
|
2502
|
-
* // Title: Handling Specific Errors with Effect.catchSome
|
|
2503
2553
|
* import { Effect, Random, Option } from "effect"
|
|
2504
2554
|
*
|
|
2505
2555
|
* class HttpError {
|
|
@@ -2538,6 +2588,8 @@ export const catchIf = core.catchIf;
|
|
|
2538
2588
|
* )
|
|
2539
2589
|
* ```
|
|
2540
2590
|
*
|
|
2591
|
+
* @see {@link catchIf} for a version that allows you to recover from errors based on a predicate.
|
|
2592
|
+
*
|
|
2541
2593
|
* @since 2.0.0
|
|
2542
2594
|
* @category Error handling
|
|
2543
2595
|
*/
|
|
@@ -2578,9 +2630,9 @@ export const catchSomeCause = effect.catchSomeCause;
|
|
|
2578
2630
|
* an `Option.some` containing the recovery logic.
|
|
2579
2631
|
* - If the defect does not match, the function returns `Option.none`, allowing the defect to propagate.
|
|
2580
2632
|
*
|
|
2581
|
-
*
|
|
2633
|
+
* **Example** (Handling Specific Defects)
|
|
2634
|
+
*
|
|
2582
2635
|
* ```ts
|
|
2583
|
-
* // Title: Handling Specific Defects
|
|
2584
2636
|
* import { Effect, Cause, Option, Console } from "effect"
|
|
2585
2637
|
*
|
|
2586
2638
|
* // Simulating a runtime error
|
|
@@ -2599,7 +2651,7 @@ export const catchSomeCause = effect.catchSomeCause;
|
|
|
2599
2651
|
*
|
|
2600
2652
|
* // Since we are only catching IllegalArgumentException
|
|
2601
2653
|
* // we will get an Exit.Failure because we simulated a runtime error.
|
|
2602
|
-
*
|
|
2654
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
2603
2655
|
* // Output:
|
|
2604
2656
|
* // {
|
|
2605
2657
|
* // _id: 'Exit',
|
|
@@ -2630,12 +2682,9 @@ export const catchSomeDefect = effect.catchSomeDefect;
|
|
|
2630
2682
|
* The error type must have a readonly `_tag` field to use `catchTag`. This
|
|
2631
2683
|
* field is used to identify and match errors.
|
|
2632
2684
|
*
|
|
2633
|
-
*
|
|
2634
|
-
* types at once.
|
|
2685
|
+
* **Example** (Handling Errors by Tag)
|
|
2635
2686
|
*
|
|
2636
|
-
* @example
|
|
2637
2687
|
* ```ts
|
|
2638
|
-
* // Title: Handling Errors by Tag
|
|
2639
2688
|
* import { Effect, Random } from "effect"
|
|
2640
2689
|
*
|
|
2641
2690
|
* class HttpError {
|
|
@@ -2670,6 +2719,9 @@ export const catchSomeDefect = effect.catchSomeDefect;
|
|
|
2670
2719
|
* )
|
|
2671
2720
|
* ```
|
|
2672
2721
|
*
|
|
2722
|
+
* @see {@link catchTags} for a version that allows you to handle multiple error
|
|
2723
|
+
* types at once.
|
|
2724
|
+
*
|
|
2673
2725
|
* @since 2.0.0
|
|
2674
2726
|
* @category Error handling
|
|
2675
2727
|
*/
|
|
@@ -2688,9 +2740,9 @@ export const catchTag = effect.catchTag;
|
|
|
2688
2740
|
* The error type must have a readonly `_tag` field to use `catchTag`. This
|
|
2689
2741
|
* field is used to identify and match errors.
|
|
2690
2742
|
*
|
|
2691
|
-
*
|
|
2743
|
+
* **Example** (Handling Multiple Tagged Error Types at Once)
|
|
2744
|
+
*
|
|
2692
2745
|
* ```ts
|
|
2693
|
-
* // Title: Handling Multiple Tagged Error Types at Once
|
|
2694
2746
|
* import { Effect, Random } from "effect"
|
|
2695
2747
|
*
|
|
2696
2748
|
* class HttpError {
|
|
@@ -2748,7 +2800,8 @@ export const catchTags = effect.catchTags;
|
|
|
2748
2800
|
* distinguishing between different types of defects (e.g., runtime exceptions,
|
|
2749
2801
|
* interruptions, etc.).
|
|
2750
2802
|
*
|
|
2751
|
-
*
|
|
2803
|
+
* **Example**
|
|
2804
|
+
*
|
|
2752
2805
|
* ```ts
|
|
2753
2806
|
* import { Effect, Console } from "effect"
|
|
2754
2807
|
*
|
|
@@ -2787,7 +2840,8 @@ export const cause = effect.cause;
|
|
|
2787
2840
|
* (e.g., a network request), and you want to keep trying without handling or
|
|
2788
2841
|
* worrying about the errors.
|
|
2789
2842
|
*
|
|
2790
|
-
*
|
|
2843
|
+
* **Example**
|
|
2844
|
+
*
|
|
2791
2845
|
* ```ts
|
|
2792
2846
|
* import { Effect } from "effect"
|
|
2793
2847
|
*
|
|
@@ -2806,7 +2860,7 @@ export const cause = effect.cause;
|
|
|
2806
2860
|
*
|
|
2807
2861
|
* const program = Effect.eventually(effect)
|
|
2808
2862
|
*
|
|
2809
|
-
*
|
|
2863
|
+
* Effect.runPromise(program).then(console.log)
|
|
2810
2864
|
* // Output:
|
|
2811
2865
|
* // running effect
|
|
2812
2866
|
* // running effect
|
|
@@ -2827,11 +2881,9 @@ export const eventually = effect.eventually;
|
|
|
2827
2881
|
* it succeeds or fails. This is useful when you only care about the side
|
|
2828
2882
|
* effects of the effect and do not need to handle or process its outcome.
|
|
2829
2883
|
*
|
|
2830
|
-
*
|
|
2884
|
+
* **Example** (Using Effect.ignore to Discard Values)
|
|
2831
2885
|
*
|
|
2832
|
-
* @example
|
|
2833
2886
|
* ```ts
|
|
2834
|
-
* // Title: Using Effect.ignore to Discard Values
|
|
2835
2887
|
* import { Effect } from "effect"
|
|
2836
2888
|
*
|
|
2837
2889
|
* // ┌─── Effect<number, string, never>
|
|
@@ -2843,6 +2895,8 @@ export const eventually = effect.eventually;
|
|
|
2843
2895
|
* const program = Effect.ignore(task)
|
|
2844
2896
|
* ```
|
|
2845
2897
|
*
|
|
2898
|
+
* @see {@link ignoreLogged} to log failures while ignoring them.
|
|
2899
|
+
*
|
|
2846
2900
|
* @since 2.0.0
|
|
2847
2901
|
* @category Error handling
|
|
2848
2902
|
*/
|
|
@@ -2884,7 +2938,8 @@ export const ignoreLogged = effect.ignoreLogged;
|
|
|
2884
2938
|
* error handling in cases where you don't need to differentiate between each
|
|
2885
2939
|
* failure, but simply want to know that multiple failures occurred.
|
|
2886
2940
|
*
|
|
2887
|
-
*
|
|
2941
|
+
* **Example**
|
|
2942
|
+
*
|
|
2888
2943
|
* ```ts
|
|
2889
2944
|
* import { Effect } from "effect"
|
|
2890
2945
|
*
|
|
@@ -2897,7 +2952,7 @@ export const ignoreLogged = effect.ignoreLogged;
|
|
|
2897
2952
|
* concurrency: "unbounded"
|
|
2898
2953
|
* }).pipe(Effect.asVoid, Effect.parallelErrors)
|
|
2899
2954
|
*
|
|
2900
|
-
*
|
|
2955
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
2901
2956
|
* // Output:
|
|
2902
2957
|
* // {
|
|
2903
2958
|
* // _id: 'Exit',
|
|
@@ -2927,9 +2982,8 @@ export const parallelErrors = effect.parallelErrors;
|
|
|
2927
2982
|
* changes using {@link unsandbox} to return to the original error-handling
|
|
2928
2983
|
* behavior.
|
|
2929
2984
|
*
|
|
2930
|
-
*
|
|
2985
|
+
* **Example**
|
|
2931
2986
|
*
|
|
2932
|
-
* @example
|
|
2933
2987
|
* ```ts
|
|
2934
2988
|
* import { Effect, Console } from "effect"
|
|
2935
2989
|
*
|
|
@@ -2961,12 +3015,14 @@ export const parallelErrors = effect.parallelErrors;
|
|
|
2961
3015
|
* // Restore the original error handling with unsandbox
|
|
2962
3016
|
* const main = Effect.unsandbox(program)
|
|
2963
3017
|
*
|
|
2964
|
-
*
|
|
3018
|
+
* Effect.runPromise(main).then(console.log)
|
|
2965
3019
|
* // Output:
|
|
2966
3020
|
* // Caught a defect: Oh uh!
|
|
2967
3021
|
* // fallback result on failure
|
|
2968
3022
|
* ```
|
|
2969
3023
|
*
|
|
3024
|
+
* @see {@link unsandbox} to restore the original error handling.
|
|
3025
|
+
*
|
|
2970
3026
|
* @since 2.0.0
|
|
2971
3027
|
* @category Error handling
|
|
2972
3028
|
*/
|
|
@@ -2992,12 +3048,9 @@ export const sandbox = effect.sandbox;
|
|
|
2992
3048
|
* can control the number of retries, the delay between them, and when to stop
|
|
2993
3049
|
* retrying.
|
|
2994
3050
|
*
|
|
2995
|
-
*
|
|
2996
|
-
* @see {@link repeat} if your retry condition is based on successful outcomes rather than errors.
|
|
3051
|
+
* **Example** (Retrying with a Fixed Delay)
|
|
2997
3052
|
*
|
|
2998
|
-
* @example
|
|
2999
3053
|
* ```ts
|
|
3000
|
-
* // Title: Retrying with a Fixed Delay
|
|
3001
3054
|
* import { Effect, Schedule } from "effect"
|
|
3002
3055
|
*
|
|
3003
3056
|
* let count = 0
|
|
@@ -3019,7 +3072,7 @@ export const sandbox = effect.sandbox;
|
|
|
3019
3072
|
*
|
|
3020
3073
|
* const repeated = Effect.retry(task, policy)
|
|
3021
3074
|
*
|
|
3022
|
-
*
|
|
3075
|
+
* Effect.runPromise(repeated).then(console.log)
|
|
3023
3076
|
* // Output:
|
|
3024
3077
|
* // failure
|
|
3025
3078
|
* // failure
|
|
@@ -3028,9 +3081,9 @@ export const sandbox = effect.sandbox;
|
|
|
3028
3081
|
* // yay!
|
|
3029
3082
|
* ```
|
|
3030
3083
|
*
|
|
3031
|
-
*
|
|
3084
|
+
* **Example** (Retrying a Task up to 5 times)
|
|
3085
|
+
*
|
|
3032
3086
|
* ```ts
|
|
3033
|
-
* // Title: Retrying a Task up to 5 times
|
|
3034
3087
|
* import { Effect } from "effect"
|
|
3035
3088
|
*
|
|
3036
3089
|
* let count = 0
|
|
@@ -3048,7 +3101,7 @@ export const sandbox = effect.sandbox;
|
|
|
3048
3101
|
* })
|
|
3049
3102
|
*
|
|
3050
3103
|
* // Retry the task up to 5 times
|
|
3051
|
-
*
|
|
3104
|
+
* Effect.runPromise(Effect.retry(task, { times: 5 })).then(console.log)
|
|
3052
3105
|
* // Output:
|
|
3053
3106
|
* // failure
|
|
3054
3107
|
* // failure
|
|
@@ -3056,9 +3109,9 @@ export const sandbox = effect.sandbox;
|
|
|
3056
3109
|
* // success
|
|
3057
3110
|
* ```
|
|
3058
3111
|
*
|
|
3059
|
-
*
|
|
3112
|
+
* **Example** (Retrying Until a Specific Condition is Met)
|
|
3113
|
+
*
|
|
3060
3114
|
* ```ts
|
|
3061
|
-
* // Title: Retrying Until a Specific Condition is Met
|
|
3062
3115
|
* import { Effect } from "effect"
|
|
3063
3116
|
*
|
|
3064
3117
|
* let count = 0
|
|
@@ -3074,7 +3127,7 @@ export const sandbox = effect.sandbox;
|
|
|
3074
3127
|
* until: (err) => err === "Error 3"
|
|
3075
3128
|
* })
|
|
3076
3129
|
*
|
|
3077
|
-
*
|
|
3130
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
3078
3131
|
* // Output:
|
|
3079
3132
|
* // Action called 1 time(s)
|
|
3080
3133
|
* // Action called 2 time(s)
|
|
@@ -3086,6 +3139,9 @@ export const sandbox = effect.sandbox;
|
|
|
3086
3139
|
* // }
|
|
3087
3140
|
* ```
|
|
3088
3141
|
*
|
|
3142
|
+
* @see {@link retryOrElse} for a version that allows you to run a fallback.
|
|
3143
|
+
* @see {@link repeat} if your retry condition is based on successful outcomes rather than errors.
|
|
3144
|
+
*
|
|
3089
3145
|
* @since 2.0.0
|
|
3090
3146
|
* @category Error handling
|
|
3091
3147
|
*/
|
|
@@ -3106,11 +3162,9 @@ export const retry = schedule_.retry_combined;
|
|
|
3106
3162
|
* This function is useful when you want to handle failures gracefully by
|
|
3107
3163
|
* specifying an alternative action after repeated failures.
|
|
3108
3164
|
*
|
|
3109
|
-
*
|
|
3165
|
+
* **Example** (Retrying with Fallback)
|
|
3110
3166
|
*
|
|
3111
|
-
* @example
|
|
3112
3167
|
* ```ts
|
|
3113
|
-
* // Title: Retrying with Fallback
|
|
3114
3168
|
* import { Effect, Schedule, Console } from "effect"
|
|
3115
3169
|
*
|
|
3116
3170
|
* let count = 0
|
|
@@ -3138,7 +3192,7 @@ export const retry = schedule_.retry_combined;
|
|
|
3138
3192
|
* () => Console.log("orElse").pipe(Effect.as("default value"))
|
|
3139
3193
|
* )
|
|
3140
3194
|
*
|
|
3141
|
-
*
|
|
3195
|
+
* Effect.runPromise(repeated).then(console.log)
|
|
3142
3196
|
* // Output:
|
|
3143
3197
|
* // failure
|
|
3144
3198
|
* // failure
|
|
@@ -3147,6 +3201,8 @@ export const retry = schedule_.retry_combined;
|
|
|
3147
3201
|
* // default value
|
|
3148
3202
|
* ```
|
|
3149
3203
|
*
|
|
3204
|
+
* @see {@link retry} for a version that does not run a fallback effect.
|
|
3205
|
+
*
|
|
3150
3206
|
* @since 2.0.0
|
|
3151
3207
|
* @category Error handling
|
|
3152
3208
|
*/
|
|
@@ -3173,12 +3229,9 @@ export {
|
|
|
3173
3229
|
* 2. If you provide a `catch` function, the error is caught and the `catch`
|
|
3174
3230
|
* function maps it to an error of type `E`.
|
|
3175
3231
|
*
|
|
3176
|
-
*
|
|
3177
|
-
* throw errors.
|
|
3232
|
+
* **Example** (Safe JSON Parsing)
|
|
3178
3233
|
*
|
|
3179
|
-
* @example
|
|
3180
3234
|
* ```ts
|
|
3181
|
-
* // Title: Safe JSON Parsing
|
|
3182
3235
|
* import { Effect } from "effect"
|
|
3183
3236
|
*
|
|
3184
3237
|
* const parse = (input: string) =>
|
|
@@ -3190,8 +3243,10 @@ export {
|
|
|
3190
3243
|
* const program = parse("")
|
|
3191
3244
|
*
|
|
3192
3245
|
* ```
|
|
3193
|
-
*
|
|
3194
|
-
*
|
|
3246
|
+
*
|
|
3247
|
+
* **Example** (Custom Error Handling)
|
|
3248
|
+
*
|
|
3249
|
+
* ```ts
|
|
3195
3250
|
* import { Effect } from "effect"
|
|
3196
3251
|
*
|
|
3197
3252
|
* const parse = (input: string) =>
|
|
@@ -3205,6 +3260,10 @@ export {
|
|
|
3205
3260
|
* // ┌─── Effect<any, Error, never>
|
|
3206
3261
|
* // ▼
|
|
3207
3262
|
* const program = parse("")
|
|
3263
|
+
* ```
|
|
3264
|
+
*
|
|
3265
|
+
* @see {@link sync} if the effectful computation is synchronous and does not
|
|
3266
|
+
* throw errors.
|
|
3208
3267
|
*
|
|
3209
3268
|
* @since 2.0.0
|
|
3210
3269
|
* @category Creating Effects
|
|
@@ -3261,11 +3320,9 @@ export const tryMapPromise = effect.tryMapPromise;
|
|
|
3261
3320
|
* An optional `AbortSignal` can be provided to allow for interruption of the
|
|
3262
3321
|
* wrapped `Promise` API.
|
|
3263
3322
|
*
|
|
3264
|
-
*
|
|
3323
|
+
* **Example** (Fetching a TODO Item)
|
|
3265
3324
|
*
|
|
3266
|
-
* @example
|
|
3267
3325
|
* ```ts
|
|
3268
|
-
* // Title: Fetching a TODO Item
|
|
3269
3326
|
* import { Effect } from "effect"
|
|
3270
3327
|
*
|
|
3271
3328
|
* const getTodo = (id: number) =>
|
|
@@ -3279,8 +3336,9 @@ export const tryMapPromise = effect.tryMapPromise;
|
|
|
3279
3336
|
* const program = getTodo(1)
|
|
3280
3337
|
* ```
|
|
3281
3338
|
*
|
|
3282
|
-
*
|
|
3283
|
-
*
|
|
3339
|
+
* **Example** (Custom Error Handling)
|
|
3340
|
+
*
|
|
3341
|
+
* ```ts
|
|
3284
3342
|
* import { Effect } from "effect"
|
|
3285
3343
|
*
|
|
3286
3344
|
* const getTodo = (id: number) =>
|
|
@@ -3293,6 +3351,9 @@ export const tryMapPromise = effect.tryMapPromise;
|
|
|
3293
3351
|
* // ┌─── Effect<Response, Error, never>
|
|
3294
3352
|
* // ▼
|
|
3295
3353
|
* const program = getTodo(1)
|
|
3354
|
+
* ```
|
|
3355
|
+
*
|
|
3356
|
+
* @see {@link promise} if the effectful computation is asynchronous and does not throw errors.
|
|
3296
3357
|
*
|
|
3297
3358
|
* @since 2.0.0
|
|
3298
3359
|
* @category Creating Effects
|
|
@@ -3350,7 +3411,8 @@ export const allowInterrupt = effect.allowInterrupt;
|
|
|
3350
3411
|
* operation can be interrupted, such as when performing asynchronous operations
|
|
3351
3412
|
* or handling cancellation.
|
|
3352
3413
|
*
|
|
3353
|
-
*
|
|
3414
|
+
* **Example**
|
|
3415
|
+
*
|
|
3354
3416
|
* ```ts
|
|
3355
3417
|
* import { Console, Effect } from "effect"
|
|
3356
3418
|
*
|
|
@@ -3364,10 +3426,10 @@ export const allowInterrupt = effect.allowInterrupt;
|
|
|
3364
3426
|
* })
|
|
3365
3427
|
* })
|
|
3366
3428
|
*
|
|
3367
|
-
*
|
|
3429
|
+
* Effect.runPromise(program)
|
|
3368
3430
|
* // Output: You can interrupt this operation.
|
|
3369
3431
|
*
|
|
3370
|
-
*
|
|
3432
|
+
* Effect.runPromise(program.pipe(Effect.uninterruptible))
|
|
3371
3433
|
* // Output: This operation cannot be interrupted.
|
|
3372
3434
|
*
|
|
3373
3435
|
* ```
|
|
@@ -3399,10 +3461,8 @@ export const checkInterruptible = core.checkInterruptible;
|
|
|
3399
3461
|
* error or trigger alternative logic. This enables faster timeout handling
|
|
3400
3462
|
* without waiting for the completion of the long-running task.
|
|
3401
3463
|
*
|
|
3402
|
-
*
|
|
3403
|
-
* @see {@link uninterruptible} for creating an uninterruptible effect.
|
|
3464
|
+
* **Example**
|
|
3404
3465
|
*
|
|
3405
|
-
* @example
|
|
3406
3466
|
* ```ts
|
|
3407
3467
|
* import { Effect } from "effect"
|
|
3408
3468
|
*
|
|
@@ -3420,7 +3480,7 @@ export const checkInterruptible = core.checkInterruptible;
|
|
|
3420
3480
|
* Effect.timeout("1 second")
|
|
3421
3481
|
* )
|
|
3422
3482
|
*
|
|
3423
|
-
*
|
|
3483
|
+
* Effect.runPromiseExit(timedEffect).then(console.log)
|
|
3424
3484
|
* // Output:
|
|
3425
3485
|
* // Start heavy processing...
|
|
3426
3486
|
* // {
|
|
@@ -3435,6 +3495,9 @@ export const checkInterruptible = core.checkInterruptible;
|
|
|
3435
3495
|
* // Heavy processing done.
|
|
3436
3496
|
* ```
|
|
3437
3497
|
*
|
|
3498
|
+
* @see {@link timeout} for a version that interrupts the effect.
|
|
3499
|
+
* @see {@link uninterruptible} for creating an uninterruptible effect.
|
|
3500
|
+
*
|
|
3438
3501
|
* @since 2.0.0
|
|
3439
3502
|
* @category Interruption
|
|
3440
3503
|
*/
|
|
@@ -3450,7 +3513,8 @@ export const disconnect = fiberRuntime.disconnect;
|
|
|
3450
3513
|
* The resulting interruption can be observed in the `Exit` type if the effect
|
|
3451
3514
|
* is run with functions like {@link runPromiseExit}.
|
|
3452
3515
|
*
|
|
3453
|
-
*
|
|
3516
|
+
* **Example**
|
|
3517
|
+
*
|
|
3454
3518
|
* ```ts
|
|
3455
3519
|
* import { Effect } from "effect"
|
|
3456
3520
|
*
|
|
@@ -3462,7 +3526,7 @@ export const disconnect = fiberRuntime.disconnect;
|
|
|
3462
3526
|
* return "some result"
|
|
3463
3527
|
* })
|
|
3464
3528
|
*
|
|
3465
|
-
*
|
|
3529
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
3466
3530
|
* // Output:
|
|
3467
3531
|
* // start
|
|
3468
3532
|
* // {
|
|
@@ -3515,9 +3579,9 @@ export const interruptibleMask = core.interruptibleMask;
|
|
|
3515
3579
|
* interrupted. This effect will be executed when the fiber is interrupted,
|
|
3516
3580
|
* allowing you to perform cleanup or other actions.
|
|
3517
3581
|
*
|
|
3518
|
-
*
|
|
3582
|
+
* **Example** (Running a Cleanup Action on Interruption)
|
|
3583
|
+
*
|
|
3519
3584
|
* ```ts
|
|
3520
|
-
* // Title: Running a Cleanup Action on Interruption
|
|
3521
3585
|
* import { Console, Effect } from "effect"
|
|
3522
3586
|
*
|
|
3523
3587
|
* // This handler is executed when the fiber is interrupted
|
|
@@ -3525,19 +3589,19 @@ export const interruptibleMask = core.interruptibleMask;
|
|
|
3525
3589
|
*
|
|
3526
3590
|
* const success = Console.log("Task completed").pipe(Effect.as("some result"), handler)
|
|
3527
3591
|
*
|
|
3528
|
-
*
|
|
3592
|
+
* Effect.runFork(success)
|
|
3529
3593
|
* // Output:
|
|
3530
3594
|
* // Task completed
|
|
3531
3595
|
*
|
|
3532
3596
|
* const failure = Console.log("Task failed").pipe(Effect.andThen(Effect.fail("some error")), handler)
|
|
3533
3597
|
*
|
|
3534
|
-
*
|
|
3598
|
+
* Effect.runFork(failure)
|
|
3535
3599
|
* // Output:
|
|
3536
3600
|
* // Task failed
|
|
3537
3601
|
*
|
|
3538
3602
|
* const interruption = Console.log("Task interrupted").pipe(Effect.andThen(Effect.interrupt), handler)
|
|
3539
3603
|
*
|
|
3540
|
-
*
|
|
3604
|
+
* Effect.runFork(interruption)
|
|
3541
3605
|
* // Output:
|
|
3542
3606
|
* // Task interrupted
|
|
3543
3607
|
* // Cleanup completed
|
|
@@ -3567,7 +3631,8 @@ export const uninterruptibleMask = core.uninterruptibleMask;
|
|
|
3567
3631
|
* Transforms a `Predicate` function into an `Effect` returning the input value if the predicate returns `true`
|
|
3568
3632
|
* or failing with specified error if the predicate fails
|
|
3569
3633
|
*
|
|
3570
|
-
*
|
|
3634
|
+
* **Example**
|
|
3635
|
+
*
|
|
3571
3636
|
* ```ts
|
|
3572
3637
|
* import { Effect } from "effect"
|
|
3573
3638
|
*
|
|
@@ -3599,15 +3664,15 @@ export const liftPredicate = effect.liftPredicate;
|
|
|
3599
3664
|
* result instead. For instance, you can replace the value produced by a
|
|
3600
3665
|
* computation with a predefined value, ignoring what was calculated before.
|
|
3601
3666
|
*
|
|
3602
|
-
*
|
|
3667
|
+
* **Example** (Replacing a Value)
|
|
3668
|
+
*
|
|
3603
3669
|
* ```ts
|
|
3604
|
-
* // Title: Replacing a Value
|
|
3605
3670
|
* import { pipe, Effect } from "effect"
|
|
3606
3671
|
*
|
|
3607
3672
|
* // Replaces the value 5 with the constant "new value"
|
|
3608
3673
|
* const program = pipe(Effect.succeed(5), Effect.as("new value"))
|
|
3609
3674
|
*
|
|
3610
|
-
*
|
|
3675
|
+
* Effect.runPromise(program).then(console.log)
|
|
3611
3676
|
* // Output: "new value"
|
|
3612
3677
|
* ```
|
|
3613
3678
|
*
|
|
@@ -3652,7 +3717,8 @@ export const asVoid = core.asVoid;
|
|
|
3652
3717
|
* error channels. The success value becomes an error, and the error value
|
|
3653
3718
|
* becomes a success.
|
|
3654
3719
|
*
|
|
3655
|
-
*
|
|
3720
|
+
* **Example**
|
|
3721
|
+
*
|
|
3656
3722
|
* ```ts
|
|
3657
3723
|
* import { Effect } from "effect"
|
|
3658
3724
|
*
|
|
@@ -3699,13 +3765,9 @@ export const flipWith = effect.flipWith;
|
|
|
3699
3765
|
* effect is not modified. Instead, a new effect is returned with the updated
|
|
3700
3766
|
* value.
|
|
3701
3767
|
*
|
|
3702
|
-
*
|
|
3703
|
-
* @see {@link mapBoth} for a version that operates on both channels.
|
|
3704
|
-
* @see {@link flatMap} or {@link andThen} for a version that can return a new effect.
|
|
3768
|
+
* **Example** (Adding a Service Charge)
|
|
3705
3769
|
*
|
|
3706
|
-
* @example
|
|
3707
3770
|
* ```ts
|
|
3708
|
-
* // Title: Adding a Service Charge
|
|
3709
3771
|
* import { pipe, Effect } from "effect"
|
|
3710
3772
|
*
|
|
3711
3773
|
* const addServiceCharge = (amount: number) => amount + 1
|
|
@@ -3717,9 +3779,14 @@ export const flipWith = effect.flipWith;
|
|
|
3717
3779
|
* Effect.map(addServiceCharge)
|
|
3718
3780
|
* )
|
|
3719
3781
|
*
|
|
3720
|
-
*
|
|
3782
|
+
* Effect.runPromise(finalAmount).then(console.log)
|
|
3721
3783
|
* // Output: 101
|
|
3722
3784
|
* ```
|
|
3785
|
+
*
|
|
3786
|
+
* @see {@link mapError} for a version that operates on the error channel.
|
|
3787
|
+
* @see {@link mapBoth} for a version that operates on both channels.
|
|
3788
|
+
* @see {@link flatMap} or {@link andThen} for a version that can return a new effect.
|
|
3789
|
+
*
|
|
3723
3790
|
* @since 2.0.0
|
|
3724
3791
|
* @category Mapping
|
|
3725
3792
|
*/
|
|
@@ -3743,7 +3810,8 @@ export const map = core.map;
|
|
|
3743
3810
|
* If the input collection is a non-empty array, the return type will match the
|
|
3744
3811
|
* input collection type.
|
|
3745
3812
|
*
|
|
3746
|
-
*
|
|
3813
|
+
* **Example**
|
|
3814
|
+
*
|
|
3747
3815
|
* ```ts
|
|
3748
3816
|
* import { Effect } from "effect"
|
|
3749
3817
|
*
|
|
@@ -3756,10 +3824,10 @@ export const map = core.map;
|
|
|
3756
3824
|
* // Apply mapAccum to transform an array of strings
|
|
3757
3825
|
* const program = Effect.mapAccum(["a", "bb", "ccc"], initialState, transformation)
|
|
3758
3826
|
*
|
|
3759
|
-
*
|
|
3760
|
-
*
|
|
3761
|
-
*
|
|
3762
|
-
*
|
|
3827
|
+
* Effect.runPromise(program).then(([finalState, transformedCollection]) => {
|
|
3828
|
+
* console.log(finalState)
|
|
3829
|
+
* console.log(transformedCollection)
|
|
3830
|
+
* })
|
|
3763
3831
|
* // Output:
|
|
3764
3832
|
* // 6
|
|
3765
3833
|
* // [ 'A', 'BB', 'CCC' ]
|
|
@@ -3779,10 +3847,8 @@ export const mapAccum = effect.mapAccum;
|
|
|
3779
3847
|
* the error and the success values without altering the overall success or
|
|
3780
3848
|
* failure status of the effect.
|
|
3781
3849
|
*
|
|
3782
|
-
*
|
|
3783
|
-
* @see {@link mapError} for a version that operates on the error channel.
|
|
3850
|
+
* **Example**
|
|
3784
3851
|
*
|
|
3785
|
-
* @example
|
|
3786
3852
|
* ```ts
|
|
3787
3853
|
* import { Effect } from "effect"
|
|
3788
3854
|
*
|
|
@@ -3798,6 +3864,9 @@ export const mapAccum = effect.mapAccum;
|
|
|
3798
3864
|
* })
|
|
3799
3865
|
* ```
|
|
3800
3866
|
*
|
|
3867
|
+
* @see {@link map} for a version that operates on the success channel.
|
|
3868
|
+
* @see {@link mapError} for a version that operates on the error channel.
|
|
3869
|
+
*
|
|
3801
3870
|
* @since 2.0.0
|
|
3802
3871
|
* @category Mapping
|
|
3803
3872
|
*/
|
|
@@ -3813,11 +3882,8 @@ export const mapBoth = core.mapBoth;
|
|
|
3813
3882
|
* keeping the original behavior of the effect's success values intact. It only
|
|
3814
3883
|
* operates on the error channel and leaves the success channel unchanged.
|
|
3815
3884
|
*
|
|
3816
|
-
*
|
|
3817
|
-
* @see {@link mapBoth} for a version that operates on both channels.
|
|
3818
|
-
* @see {@link orElseFail} if you want to replace the error with a new one.
|
|
3885
|
+
* **Example**
|
|
3819
3886
|
*
|
|
3820
|
-
* @example
|
|
3821
3887
|
* ```ts
|
|
3822
3888
|
* import { Effect } from "effect"
|
|
3823
3889
|
*
|
|
@@ -3833,6 +3899,10 @@ export const mapBoth = core.mapBoth;
|
|
|
3833
3899
|
* )
|
|
3834
3900
|
* ```
|
|
3835
3901
|
*
|
|
3902
|
+
* @see {@link map} for a version that operates on the success channel.
|
|
3903
|
+
* @see {@link mapBoth} for a version that operates on both channels.
|
|
3904
|
+
* @see {@link orElseFail} if you want to replace the error with a new one.
|
|
3905
|
+
*
|
|
3836
3906
|
* @since 2.0.0
|
|
3837
3907
|
* @category Mapping
|
|
3838
3908
|
*/
|
|
@@ -3862,7 +3932,8 @@ export const mapErrorCause = effect.mapErrorCause;
|
|
|
3862
3932
|
* error type and still capture both successful results and errors as part of
|
|
3863
3933
|
* the outcome.
|
|
3864
3934
|
*
|
|
3865
|
-
*
|
|
3935
|
+
* **Example**
|
|
3936
|
+
*
|
|
3866
3937
|
* ```ts
|
|
3867
3938
|
* import { Effect } from "effect"
|
|
3868
3939
|
*
|
|
@@ -3921,11 +3992,9 @@ export const negate = effect.negate;
|
|
|
3921
3992
|
* etc.) will not be interrupted, and that the resource will always be released
|
|
3922
3993
|
* when the `Effect` completes execution.
|
|
3923
3994
|
*
|
|
3924
|
-
*
|
|
3995
|
+
* **Example** (Defining a Simple Resource)
|
|
3925
3996
|
*
|
|
3926
|
-
* @example
|
|
3927
3997
|
* ```ts
|
|
3928
|
-
* // Title: Defining a Simple Resource
|
|
3929
3998
|
* import { Effect } from "effect"
|
|
3930
3999
|
*
|
|
3931
4000
|
* // Define an interface for a resource
|
|
@@ -3965,6 +4034,8 @@ export const negate = effect.negate;
|
|
|
3965
4034
|
* const resource = Effect.acquireRelease(acquire, release)
|
|
3966
4035
|
* ```
|
|
3967
4036
|
*
|
|
4037
|
+
* @see {@link acquireUseRelease} for a version that automatically handles the scoping of resources.
|
|
4038
|
+
*
|
|
3968
4039
|
* @since 2.0.0
|
|
3969
4040
|
* @category Scoping, Resources & Finalization
|
|
3970
4041
|
*/
|
|
@@ -3984,23 +4055,21 @@ export const acquireRelease = fiberRuntime.acquireRelease;
|
|
|
3984
4055
|
*/
|
|
3985
4056
|
export const acquireReleaseInterruptible = fiberRuntime.acquireReleaseInterruptible;
|
|
3986
4057
|
/**
|
|
3987
|
-
*
|
|
3988
|
-
* scope.
|
|
4058
|
+
* Many real-world operations involve working with resources that must be released when no longer needed, such as:
|
|
3989
4059
|
*
|
|
3990
|
-
*
|
|
4060
|
+
* - Database connections
|
|
4061
|
+
* - File handles
|
|
4062
|
+
* - Network requests
|
|
3991
4063
|
*
|
|
3992
|
-
* This function
|
|
3993
|
-
* additional `use` effect. This allows you to automatically execute the `use`
|
|
3994
|
-
* effect while the resource is acquired, and it also ensures that the `release`
|
|
3995
|
-
* effect is performed when the scope is closed.
|
|
4064
|
+
* This function ensures that a resource is:
|
|
3996
4065
|
*
|
|
3997
|
-
*
|
|
3998
|
-
*
|
|
3999
|
-
*
|
|
4066
|
+
* 1. **Acquired** properly.
|
|
4067
|
+
* 2. **Used** for its intended purpose.
|
|
4068
|
+
* 3. **Released** even if an error occurs.
|
|
4069
|
+
*
|
|
4070
|
+
* **Example** (Automatically Managing Resource Lifetime)
|
|
4000
4071
|
*
|
|
4001
|
-
* @example
|
|
4002
4072
|
* ```ts
|
|
4003
|
-
* // Title: Automatically Managing Resource Lifetime
|
|
4004
4073
|
* import { Effect, Console } from "effect"
|
|
4005
4074
|
*
|
|
4006
4075
|
* // Define an interface for a resource
|
|
@@ -4039,7 +4108,7 @@ export const acquireReleaseInterruptible = fiberRuntime.acquireReleaseInterrupti
|
|
|
4039
4108
|
* // ▼
|
|
4040
4109
|
* const program = Effect.acquireUseRelease(acquire, use, release)
|
|
4041
4110
|
*
|
|
4042
|
-
*
|
|
4111
|
+
* Effect.runPromise(program)
|
|
4043
4112
|
* // Output:
|
|
4044
4113
|
* // Resource acquired
|
|
4045
4114
|
* // content is lorem ipsum
|
|
@@ -4069,11 +4138,9 @@ export const acquireUseRelease = core.acquireUseRelease;
|
|
|
4069
4138
|
* effect with a finalizer is wrapped in a scope, the finalizer will execute
|
|
4070
4139
|
* automatically when the scope ends.
|
|
4071
4140
|
*
|
|
4072
|
-
*
|
|
4141
|
+
* **Example** (Adding a Finalizer on Success)
|
|
4073
4142
|
*
|
|
4074
|
-
* @example
|
|
4075
4143
|
* ```ts
|
|
4076
|
-
* // Title: Adding a Finalizer on Success
|
|
4077
4144
|
* import { Effect, Console } from "effect"
|
|
4078
4145
|
*
|
|
4079
4146
|
* // ┌─── Effect<string, never, Scope>
|
|
@@ -4091,15 +4158,15 @@ export const acquireUseRelease = core.acquireUseRelease;
|
|
|
4091
4158
|
* // ▼
|
|
4092
4159
|
* const runnable = Effect.scoped(program)
|
|
4093
4160
|
*
|
|
4094
|
-
*
|
|
4161
|
+
* Effect.runPromiseExit(runnable).then(console.log)
|
|
4095
4162
|
* // Output:
|
|
4096
4163
|
* // Finalizer executed. Exit status: Success
|
|
4097
4164
|
* // { _id: 'Exit', _tag: 'Success', value: 'some result' }
|
|
4098
4165
|
* ```
|
|
4099
4166
|
*
|
|
4100
|
-
*
|
|
4167
|
+
* **Example** (Adding a Finalizer on Failure)
|
|
4168
|
+
*
|
|
4101
4169
|
* ```ts
|
|
4102
|
-
* // Title: Adding a Finalizer on Failure
|
|
4103
4170
|
* import { Effect, Console } from "effect"
|
|
4104
4171
|
*
|
|
4105
4172
|
* // ┌─── Effect<never, string, Scope>
|
|
@@ -4117,7 +4184,7 @@ export const acquireUseRelease = core.acquireUseRelease;
|
|
|
4117
4184
|
* // ▼
|
|
4118
4185
|
* const runnable = Effect.scoped(program)
|
|
4119
4186
|
*
|
|
4120
|
-
*
|
|
4187
|
+
* Effect.runPromiseExit(runnable).then(console.log)
|
|
4121
4188
|
* // Output:
|
|
4122
4189
|
* // Finalizer executed. Exit status: Failure
|
|
4123
4190
|
* // {
|
|
@@ -4127,9 +4194,9 @@ export const acquireUseRelease = core.acquireUseRelease;
|
|
|
4127
4194
|
* // }
|
|
4128
4195
|
* ```
|
|
4129
4196
|
*
|
|
4130
|
-
*
|
|
4197
|
+
* **Example** (Adding a Finalizer on Interruption)
|
|
4198
|
+
*
|
|
4131
4199
|
* ```ts
|
|
4132
|
-
* // Title: Adding a Finalizer on Interruption
|
|
4133
4200
|
* import { Effect, Console } from "effect"
|
|
4134
4201
|
*
|
|
4135
4202
|
* // ┌─── Effect<never, never, Scope>
|
|
@@ -4147,7 +4214,7 @@ export const acquireUseRelease = core.acquireUseRelease;
|
|
|
4147
4214
|
* // ▼
|
|
4148
4215
|
* const runnable = Effect.scoped(program)
|
|
4149
4216
|
*
|
|
4150
|
-
*
|
|
4217
|
+
* Effect.runPromiseExit(runnable).then(console.log)
|
|
4151
4218
|
* // Output:
|
|
4152
4219
|
* // Finalizer executed. Exit status: Failure
|
|
4153
4220
|
* // {
|
|
@@ -4166,6 +4233,8 @@ export const acquireUseRelease = core.acquireUseRelease;
|
|
|
4166
4233
|
* // }
|
|
4167
4234
|
* ```
|
|
4168
4235
|
*
|
|
4236
|
+
* @see {@link onExit} for attaching a finalizer directly to an effect.
|
|
4237
|
+
*
|
|
4169
4238
|
* @since 2.0.0
|
|
4170
4239
|
* @category Scoping, Resources & Finalization
|
|
4171
4240
|
*/
|
|
@@ -4192,37 +4261,51 @@ export const addFinalizer = fiberRuntime.addFinalizer;
|
|
|
4192
4261
|
* For use cases where you need access to the result of an effect, consider
|
|
4193
4262
|
* using {@link onExit}.
|
|
4194
4263
|
*
|
|
4195
|
-
*
|
|
4196
|
-
* effect.
|
|
4264
|
+
* **Example** (Running a Finalizer in All Outcomes)
|
|
4197
4265
|
*
|
|
4198
|
-
* @example
|
|
4199
4266
|
* ```ts
|
|
4200
4267
|
* import { Console, Effect } from "effect"
|
|
4201
4268
|
*
|
|
4269
|
+
* // Define a cleanup effect
|
|
4202
4270
|
* const handler = Effect.ensuring(Console.log("Cleanup completed"))
|
|
4203
4271
|
*
|
|
4204
|
-
*
|
|
4272
|
+
* // Define a successful effect
|
|
4273
|
+
* const success = Console.log("Task completed").pipe(
|
|
4274
|
+
* Effect.as("some result"),
|
|
4275
|
+
* handler
|
|
4276
|
+
* )
|
|
4205
4277
|
*
|
|
4206
|
-
*
|
|
4278
|
+
* Effect.runFork(success)
|
|
4207
4279
|
* // Output:
|
|
4208
4280
|
* // Task completed
|
|
4209
4281
|
* // Cleanup completed
|
|
4210
4282
|
*
|
|
4211
|
-
*
|
|
4283
|
+
* // Define a failing effect
|
|
4284
|
+
* const failure = Console.log("Task failed").pipe(
|
|
4285
|
+
* Effect.andThen(Effect.fail("some error")),
|
|
4286
|
+
* handler
|
|
4287
|
+
* )
|
|
4212
4288
|
*
|
|
4213
|
-
*
|
|
4289
|
+
* Effect.runFork(failure)
|
|
4214
4290
|
* // Output:
|
|
4215
4291
|
* // Task failed
|
|
4216
4292
|
* // Cleanup completed
|
|
4217
4293
|
*
|
|
4218
|
-
*
|
|
4294
|
+
* // Define an interrupted effect
|
|
4295
|
+
* const interruption = Console.log("Task interrupted").pipe(
|
|
4296
|
+
* Effect.andThen(Effect.interrupt),
|
|
4297
|
+
* handler
|
|
4298
|
+
* )
|
|
4219
4299
|
*
|
|
4220
|
-
*
|
|
4300
|
+
* Effect.runFork(interruption)
|
|
4221
4301
|
* // Output:
|
|
4222
4302
|
* // Task interrupted
|
|
4223
4303
|
* // Cleanup completed
|
|
4224
4304
|
* ```
|
|
4225
4305
|
*
|
|
4306
|
+
* @see {@link onExit} for a version that provides access to the result of an
|
|
4307
|
+
* effect.
|
|
4308
|
+
*
|
|
4226
4309
|
* @since 2.0.0
|
|
4227
4310
|
* @category Scoping, Resources & Finalization
|
|
4228
4311
|
*/
|
|
@@ -4242,37 +4325,63 @@ export const ensuring = fiberRuntime.ensuring;
|
|
|
4242
4325
|
* Importantly, the cleanup effect itself is uninterruptible, ensuring that it
|
|
4243
4326
|
* completes regardless of external interruptions.
|
|
4244
4327
|
*
|
|
4245
|
-
*
|
|
4246
|
-
* @see {@link onExit} for attaching a cleanup effect that runs on all possible exits.
|
|
4328
|
+
* **Example** (Running Cleanup Only on Failure)
|
|
4247
4329
|
*
|
|
4248
|
-
* @example
|
|
4249
4330
|
* ```ts
|
|
4250
4331
|
* import { Console, Effect } from "effect"
|
|
4251
4332
|
*
|
|
4252
4333
|
* // This handler logs the failure cause when the effect fails
|
|
4253
|
-
* const handler = Effect.onError((cause) =>
|
|
4334
|
+
* const handler = Effect.onError((cause) =>
|
|
4335
|
+
* Console.log(`Cleanup completed: ${cause}`)
|
|
4336
|
+
* )
|
|
4254
4337
|
*
|
|
4255
|
-
*
|
|
4338
|
+
* // Define a successful effect
|
|
4339
|
+
* const success = Console.log("Task completed").pipe(
|
|
4340
|
+
* Effect.as("some result"),
|
|
4341
|
+
* handler
|
|
4342
|
+
* )
|
|
4256
4343
|
*
|
|
4257
|
-
*
|
|
4344
|
+
* Effect.runFork(success)
|
|
4258
4345
|
* // Output:
|
|
4259
4346
|
* // Task completed
|
|
4260
4347
|
*
|
|
4261
|
-
*
|
|
4348
|
+
* // Define a failing effect
|
|
4349
|
+
* const failure = Console.log("Task failed").pipe(
|
|
4350
|
+
* Effect.andThen(Effect.fail("some error")),
|
|
4351
|
+
* handler
|
|
4352
|
+
* )
|
|
4262
4353
|
*
|
|
4263
|
-
*
|
|
4354
|
+
* Effect.runFork(failure)
|
|
4264
4355
|
* // Output:
|
|
4265
4356
|
* // Task failed
|
|
4266
4357
|
* // Cleanup completed: Error: some error
|
|
4267
4358
|
*
|
|
4268
|
-
*
|
|
4359
|
+
* // Define a failing effect
|
|
4360
|
+
* const defect = Console.log("Task failed with defect").pipe(
|
|
4361
|
+
* Effect.andThen(Effect.die("Boom!")),
|
|
4362
|
+
* handler
|
|
4363
|
+
* )
|
|
4364
|
+
*
|
|
4365
|
+
* Effect.runFork(defect)
|
|
4366
|
+
* // Output:
|
|
4367
|
+
* // Task failed with defect
|
|
4368
|
+
* // Cleanup completed: Error: Boom!
|
|
4369
|
+
*
|
|
4370
|
+
* // Define an interrupted effect
|
|
4371
|
+
* const interruption = Console.log("Task interrupted").pipe(
|
|
4372
|
+
* Effect.andThen(Effect.interrupt),
|
|
4373
|
+
* handler
|
|
4374
|
+
* )
|
|
4269
4375
|
*
|
|
4270
|
-
*
|
|
4376
|
+
* Effect.runFork(interruption)
|
|
4271
4377
|
* // Output:
|
|
4272
4378
|
* // Task interrupted
|
|
4273
4379
|
* // Cleanup completed: All fibers interrupted without errors.
|
|
4274
4380
|
* ```
|
|
4275
4381
|
*
|
|
4382
|
+
* @see {@link ensuring} for attaching a cleanup effect that runs on both success and failure.
|
|
4383
|
+
* @see {@link onExit} for attaching a cleanup effect that runs on all possible exits.
|
|
4384
|
+
*
|
|
4276
4385
|
* @since 2.0.0
|
|
4277
4386
|
* @category Scoping, Resources & Finalization
|
|
4278
4387
|
*/
|
|
@@ -4294,30 +4403,45 @@ export const onError = core.onError;
|
|
|
4294
4403
|
* The cleanup function is guaranteed to run uninterruptibly, ensuring reliable
|
|
4295
4404
|
* resource management even in complex or high-concurrency scenarios.
|
|
4296
4405
|
*
|
|
4297
|
-
*
|
|
4406
|
+
* **Example** (Running a Cleanup Function with the Effect’s Result)
|
|
4407
|
+
*
|
|
4298
4408
|
* ```ts
|
|
4299
4409
|
* import { Console, Effect, Exit } from "effect"
|
|
4300
4410
|
*
|
|
4301
|
-
* // Define a cleanup
|
|
4302
|
-
* const handler = Effect.onExit((exit) =>
|
|
4411
|
+
* // Define a cleanup effect that logs the result
|
|
4412
|
+
* const handler = Effect.onExit((exit) =>
|
|
4413
|
+
* Console.log(`Cleanup completed: ${Exit.getOrElse(exit, String)}`)
|
|
4414
|
+
* )
|
|
4303
4415
|
*
|
|
4304
|
-
*
|
|
4416
|
+
* // Define a successful effect
|
|
4417
|
+
* const success = Console.log("Task completed").pipe(
|
|
4418
|
+
* Effect.as("some result"),
|
|
4419
|
+
* handler
|
|
4420
|
+
* )
|
|
4305
4421
|
*
|
|
4306
|
-
*
|
|
4422
|
+
* Effect.runFork(success)
|
|
4307
4423
|
* // Output:
|
|
4308
4424
|
* // Task completed
|
|
4309
4425
|
* // Cleanup completed: some result
|
|
4310
4426
|
*
|
|
4311
|
-
*
|
|
4427
|
+
* // Define a failing effect
|
|
4428
|
+
* const failure = Console.log("Task failed").pipe(
|
|
4429
|
+
* Effect.andThen(Effect.fail("some error")),
|
|
4430
|
+
* handler
|
|
4431
|
+
* )
|
|
4312
4432
|
*
|
|
4313
|
-
*
|
|
4433
|
+
* Effect.runFork(failure)
|
|
4314
4434
|
* // Output:
|
|
4315
4435
|
* // Task failed
|
|
4316
4436
|
* // Cleanup completed: Error: some error
|
|
4317
4437
|
*
|
|
4318
|
-
*
|
|
4438
|
+
* // Define an interrupted effect
|
|
4439
|
+
* const interruption = Console.log("Task interrupted").pipe(
|
|
4440
|
+
* Effect.andThen(Effect.interrupt),
|
|
4441
|
+
* handler
|
|
4442
|
+
* )
|
|
4319
4443
|
*
|
|
4320
|
-
*
|
|
4444
|
+
* Effect.runFork(interruption)
|
|
4321
4445
|
* // Output:
|
|
4322
4446
|
* // Task interrupted
|
|
4323
4447
|
* // Cleanup completed: All fibers interrupted without errors.
|
|
@@ -4347,9 +4471,8 @@ export const onExit = core.onExit;
|
|
|
4347
4471
|
* these tasks do not depend on the order of execution or introduce race
|
|
4348
4472
|
* conditions.
|
|
4349
4473
|
*
|
|
4350
|
-
*
|
|
4474
|
+
* **Example**
|
|
4351
4475
|
*
|
|
4352
|
-
* @example
|
|
4353
4476
|
* ```ts
|
|
4354
4477
|
* import { Console, Effect } from "effect"
|
|
4355
4478
|
*
|
|
@@ -4366,13 +4489,15 @@ export const onExit = core.onExit;
|
|
|
4366
4489
|
*
|
|
4367
4490
|
* const runnable = Effect.scoped(modified)
|
|
4368
4491
|
*
|
|
4369
|
-
*
|
|
4492
|
+
* Effect.runFork(runnable)
|
|
4370
4493
|
* // Output:
|
|
4371
4494
|
* // Finalizer 2 executed
|
|
4372
4495
|
* // Finalizer 3 executed
|
|
4373
4496
|
* // Finalizer 1 executed
|
|
4374
4497
|
* ```
|
|
4375
4498
|
*
|
|
4499
|
+
* @see {@link sequentialFinalizers} for a version that ensures finalizers are run sequentially.
|
|
4500
|
+
*
|
|
4376
4501
|
* @since 2.0.0
|
|
4377
4502
|
* @category Scoping, Resources & Finalization
|
|
4378
4503
|
*/
|
|
@@ -4459,9 +4584,8 @@ export const scoped = fiberRuntime.scopedEffect;
|
|
|
4459
4584
|
* resources are cleaned up as soon as the `use` effect completes, regardless of
|
|
4460
4585
|
* how the `use` effect ends (success, failure, or interruption).
|
|
4461
4586
|
*
|
|
4462
|
-
*
|
|
4587
|
+
* **Example**
|
|
4463
4588
|
*
|
|
4464
|
-
* @example
|
|
4465
4589
|
* ```ts
|
|
4466
4590
|
* import { Console, Effect } from "effect"
|
|
4467
4591
|
*
|
|
@@ -4473,13 +4597,15 @@ export const scoped = fiberRuntime.scopedEffect;
|
|
|
4473
4597
|
*
|
|
4474
4598
|
* const program = acquire.pipe(Effect.using(use))
|
|
4475
4599
|
*
|
|
4476
|
-
*
|
|
4600
|
+
* Effect.runFork(program)
|
|
4477
4601
|
* // Output:
|
|
4478
4602
|
* // Acquiring resource
|
|
4479
4603
|
* // Using resource: 1
|
|
4480
4604
|
* // Releasing resource
|
|
4481
4605
|
* ```
|
|
4482
4606
|
*
|
|
4607
|
+
* @see {@link scopedWith} Manage scoped operations with a temporary scope.
|
|
4608
|
+
*
|
|
4483
4609
|
* @since 2.0.0
|
|
4484
4610
|
* @category Scoping, Resources & Finalization
|
|
4485
4611
|
*/
|
|
@@ -4494,7 +4620,8 @@ export const using = fiberRuntime.using;
|
|
|
4494
4620
|
* workflows where you need early access to the result while retaining control
|
|
4495
4621
|
* over the resource cleanup process.
|
|
4496
4622
|
*
|
|
4497
|
-
*
|
|
4623
|
+
* **Example**
|
|
4624
|
+
*
|
|
4498
4625
|
* ```ts
|
|
4499
4626
|
* import { Console, Effect } from "effect"
|
|
4500
4627
|
*
|
|
@@ -4612,9 +4739,8 @@ export const fiberIdWith = core.fiberIdWith;
|
|
|
4612
4739
|
* higher-level functions like {@link raceWith}, {@link zip}, or others that can
|
|
4613
4740
|
* manage concurrency for you.
|
|
4614
4741
|
*
|
|
4615
|
-
*
|
|
4742
|
+
* **Example**
|
|
4616
4743
|
*
|
|
4617
|
-
* @example
|
|
4618
4744
|
* ```ts
|
|
4619
4745
|
* import { Effect } from "effect"
|
|
4620
4746
|
*
|
|
@@ -4628,6 +4754,8 @@ export const fiberIdWith = core.fiberIdWith;
|
|
|
4628
4754
|
* const fib10Fiber = Effect.fork(fib(10))
|
|
4629
4755
|
* ```
|
|
4630
4756
|
*
|
|
4757
|
+
* @see {@link forkWithErrorHandler} for a version that allows you to handle errors.
|
|
4758
|
+
*
|
|
4631
4759
|
* @since 2.0.0
|
|
4632
4760
|
* @category Supervision & Fibers
|
|
4633
4761
|
*/
|
|
@@ -4644,9 +4772,9 @@ export const fork = fiberRuntime.fork;
|
|
|
4644
4772
|
* useful for tasks that need to run in the background independently, such as
|
|
4645
4773
|
* periodic logging, monitoring, or background data processing.
|
|
4646
4774
|
*
|
|
4647
|
-
*
|
|
4775
|
+
* **Example** (Creating a Daemon Fibe)
|
|
4776
|
+
*
|
|
4648
4777
|
* ```ts
|
|
4649
|
-
* // Title: Creating a Daemon Fibe
|
|
4650
4778
|
* import { Effect, Console, Schedule } from "effect"
|
|
4651
4779
|
*
|
|
4652
4780
|
* // Daemon fiber that logs a message repeatedly every second
|
|
@@ -4663,7 +4791,7 @@ export const fork = fiberRuntime.fork;
|
|
|
4663
4791
|
* console.log("parent: finished!")
|
|
4664
4792
|
* })
|
|
4665
4793
|
*
|
|
4666
|
-
*
|
|
4794
|
+
* Effect.runFork(parent)
|
|
4667
4795
|
* // Output:
|
|
4668
4796
|
* // parent: started!
|
|
4669
4797
|
* // daemon: still running!
|
|
@@ -4702,15 +4830,13 @@ export const forkAll = circular.forkAll;
|
|
|
4702
4830
|
*
|
|
4703
4831
|
* The fiber will be interrupted when the scope is closed.
|
|
4704
4832
|
*
|
|
4705
|
-
*
|
|
4706
|
-
*
|
|
4707
|
-
*
|
|
4708
|
-
*
|
|
4709
|
-
*
|
|
4710
|
-
* // allowing it to outlive the inner scope but still be terminated
|
|
4711
|
-
* // when the outerScope is closed.
|
|
4712
|
-
* //
|
|
4833
|
+
* **Example** (Forking a Fiber in a Specific Scope)
|
|
4834
|
+
*
|
|
4835
|
+
* In this example, the child fiber is forked into the outerScope,
|
|
4836
|
+
* allowing it to outlive the inner scope but still be terminated
|
|
4837
|
+
* when the outerScope is closed.
|
|
4713
4838
|
*
|
|
4839
|
+
* ```ts
|
|
4714
4840
|
* import { Console, Effect, Schedule } from "effect"
|
|
4715
4841
|
*
|
|
4716
4842
|
* // Child fiber that logs a message repeatedly every second
|
|
@@ -4744,7 +4870,7 @@ export const forkAll = circular.forkAll;
|
|
|
4744
4870
|
* })
|
|
4745
4871
|
* )
|
|
4746
4872
|
*
|
|
4747
|
-
*
|
|
4873
|
+
* Effect.runFork(program)
|
|
4748
4874
|
* // Output:
|
|
4749
4875
|
* // child: still running!
|
|
4750
4876
|
* // child: still running!
|
|
@@ -4780,14 +4906,12 @@ export const forkIn = circular.forkIn;
|
|
|
4780
4906
|
* terminates. With `forkScoped`, the child fiber will keep running until the
|
|
4781
4907
|
* local scope ends, regardless of the state of the parent fiber.
|
|
4782
4908
|
*
|
|
4783
|
-
*
|
|
4784
|
-
*
|
|
4785
|
-
*
|
|
4786
|
-
*
|
|
4787
|
-
* // In this example, the child fiber continues to run beyond the lifetime of the parent fiber.
|
|
4788
|
-
* // The child fiber is tied to the local scope and will be terminated only when the scope ends.
|
|
4789
|
-
* //
|
|
4909
|
+
* **Example** (Forking a Fiber in a Local Scope)
|
|
4910
|
+
*
|
|
4911
|
+
* In this example, the child fiber continues to run beyond the lifetime of the parent fiber.
|
|
4912
|
+
* The child fiber is tied to the local scope and will be terminated only when the scope ends.
|
|
4790
4913
|
*
|
|
4914
|
+
* ```ts
|
|
4791
4915
|
* import { Effect, Console, Schedule } from "effect"
|
|
4792
4916
|
*
|
|
4793
4917
|
* // Child fiber that logs a message repeatedly every second
|
|
@@ -4817,7 +4941,7 @@ export const forkIn = circular.forkIn;
|
|
|
4817
4941
|
* })
|
|
4818
4942
|
* )
|
|
4819
4943
|
*
|
|
4820
|
-
*
|
|
4944
|
+
* Effect.runFork(program)
|
|
4821
4945
|
* // Output:
|
|
4822
4946
|
* // Local scope started!
|
|
4823
4947
|
* // parent: started!
|
|
@@ -4871,9 +4995,9 @@ export const fromFiberEffect = circular.fromFiberEffect;
|
|
|
4871
4995
|
* This enables you to capture detailed information about these child fibers,
|
|
4872
4996
|
* such as their status, through the supervisor.
|
|
4873
4997
|
*
|
|
4874
|
-
*
|
|
4998
|
+
* **Example** (Monitoring Fiber Count)
|
|
4999
|
+
*
|
|
4875
5000
|
* ```ts
|
|
4876
|
-
* // Title: Monitoring Fiber Count
|
|
4877
5001
|
* import { Effect, Supervisor, Schedule, Fiber, FiberStatus } from "effect"
|
|
4878
5002
|
*
|
|
4879
5003
|
* // Main program that monitors fibers while calculating a Fibonacci number
|
|
@@ -4941,7 +5065,7 @@ export const fromFiberEffect = circular.fromFiberEffect;
|
|
|
4941
5065
|
* return v1 + v2 // Combine the results
|
|
4942
5066
|
* })
|
|
4943
5067
|
*
|
|
4944
|
-
*
|
|
5068
|
+
* Effect.runPromise(program)
|
|
4945
5069
|
* // Output:
|
|
4946
5070
|
* // number of fibers: 0
|
|
4947
5071
|
* // number of fibers: 2
|
|
@@ -5011,7 +5135,8 @@ export const withMaxOpsBeforeYield = core.withMaxOpsBeforeYield;
|
|
|
5011
5135
|
/**
|
|
5012
5136
|
* Retrieves the `Clock` service from the context.
|
|
5013
5137
|
*
|
|
5014
|
-
*
|
|
5138
|
+
* **Example**
|
|
5139
|
+
*
|
|
5015
5140
|
* ```ts
|
|
5016
5141
|
* import { Effect } from "effect"
|
|
5017
5142
|
*
|
|
@@ -5021,7 +5146,7 @@ export const withMaxOpsBeforeYield = core.withMaxOpsBeforeYield;
|
|
|
5021
5146
|
* console.log(`Current time in milliseconds: ${currentTime}`)
|
|
5022
5147
|
* })
|
|
5023
5148
|
*
|
|
5024
|
-
*
|
|
5149
|
+
* Effect.runFork(program)
|
|
5025
5150
|
* // Example Output:
|
|
5026
5151
|
* // Current time in milliseconds: 1735484796134
|
|
5027
5152
|
* ```
|
|
@@ -5034,7 +5159,8 @@ export const clock = effect.clock;
|
|
|
5034
5159
|
* Retrieves the `Clock` service from the context and provides it to the
|
|
5035
5160
|
* specified effectful function.
|
|
5036
5161
|
*
|
|
5037
|
-
*
|
|
5162
|
+
* **Example**
|
|
5163
|
+
*
|
|
5038
5164
|
* ```ts
|
|
5039
5165
|
* import { Console, Effect } from "effect"
|
|
5040
5166
|
*
|
|
@@ -5045,7 +5171,7 @@ export const clock = effect.clock;
|
|
|
5045
5171
|
* )
|
|
5046
5172
|
* )
|
|
5047
5173
|
*
|
|
5048
|
-
*
|
|
5174
|
+
* Effect.runFork(program)
|
|
5049
5175
|
* // Example Output:
|
|
5050
5176
|
* // Current time is: 1735484929744
|
|
5051
5177
|
* ```
|
|
@@ -5113,7 +5239,8 @@ export const withConsole = console_.withConsole;
|
|
|
5113
5239
|
* Internally, this function does not block the thread; instead, it uses an
|
|
5114
5240
|
* efficient, non-blocking mechanism to introduce the delay.
|
|
5115
5241
|
*
|
|
5116
|
-
*
|
|
5242
|
+
* **Example**
|
|
5243
|
+
*
|
|
5117
5244
|
* ```ts
|
|
5118
5245
|
* import { Console, Effect } from "effect"
|
|
5119
5246
|
*
|
|
@@ -5126,7 +5253,7 @@ export const withConsole = console_.withConsole;
|
|
|
5126
5253
|
* )
|
|
5127
5254
|
* )
|
|
5128
5255
|
*
|
|
5129
|
-
*
|
|
5256
|
+
* Effect.runFork(program)
|
|
5130
5257
|
* // Output:
|
|
5131
5258
|
* // start
|
|
5132
5259
|
* // Task executed
|
|
@@ -5150,7 +5277,8 @@ export const delay = effect.delay;
|
|
|
5150
5277
|
* `Duration` module, such as a string (`"2 seconds"`) or numeric value
|
|
5151
5278
|
* representing milliseconds.
|
|
5152
5279
|
*
|
|
5153
|
-
*
|
|
5280
|
+
* **Example**
|
|
5281
|
+
*
|
|
5154
5282
|
* ```ts
|
|
5155
5283
|
* import { Effect } from "effect"
|
|
5156
5284
|
*
|
|
@@ -5160,7 +5288,7 @@ export const delay = effect.delay;
|
|
|
5160
5288
|
* console.log("Task completed!")
|
|
5161
5289
|
* })
|
|
5162
5290
|
*
|
|
5163
|
-
*
|
|
5291
|
+
* Effect.runFork(program)
|
|
5164
5292
|
* // Output:
|
|
5165
5293
|
* // Starting task...
|
|
5166
5294
|
* // Task completed!
|
|
@@ -5186,7 +5314,8 @@ export const sleep = effect.sleep;
|
|
|
5186
5314
|
* unchanged, and the timing information is provided alongside the result in a
|
|
5187
5315
|
* tuple.
|
|
5188
5316
|
*
|
|
5189
|
-
*
|
|
5317
|
+
* **Example**
|
|
5318
|
+
*
|
|
5190
5319
|
* ```ts
|
|
5191
5320
|
* import { Duration, Effect } from "effect"
|
|
5192
5321
|
*
|
|
@@ -5202,7 +5331,7 @@ export const sleep = effect.sleep;
|
|
|
5202
5331
|
* console.log(`Task completed in ${Duration.toMillis(duration)} ms with result: ${result}`)
|
|
5203
5332
|
* })
|
|
5204
5333
|
*
|
|
5205
|
-
*
|
|
5334
|
+
* Effect.runFork(program)
|
|
5206
5335
|
* // Output: Task completed in 2003.749125 ms with result: some result
|
|
5207
5336
|
* ```
|
|
5208
5337
|
*
|
|
@@ -5242,12 +5371,8 @@ export const timedWith = effect.timedWith;
|
|
|
5242
5371
|
* specified duration.
|
|
5243
5372
|
* - Fail with a `TimeoutException` if the time limit is exceeded.
|
|
5244
5373
|
*
|
|
5245
|
-
*
|
|
5246
|
-
* @see {@link timeoutFailCause} for a version that raises a custom defect.
|
|
5247
|
-
* @see {@link timeoutTo} for a version that allows specifying both success and
|
|
5248
|
-
* timeout handlers.
|
|
5374
|
+
* **Example**
|
|
5249
5375
|
*
|
|
5250
|
-
* @example
|
|
5251
5376
|
* ```ts
|
|
5252
5377
|
* import { Effect } from "effect"
|
|
5253
5378
|
*
|
|
@@ -5262,7 +5387,7 @@ export const timedWith = effect.timedWith;
|
|
|
5262
5387
|
* // than the specified timeout duration
|
|
5263
5388
|
* const timedEffect = task.pipe(Effect.timeout("1 second"))
|
|
5264
5389
|
*
|
|
5265
|
-
*
|
|
5390
|
+
* Effect.runPromiseExit(timedEffect).then(console.log)
|
|
5266
5391
|
* // Output:
|
|
5267
5392
|
* // Start processing...
|
|
5268
5393
|
* // {
|
|
@@ -5276,6 +5401,11 @@ export const timedWith = effect.timedWith;
|
|
|
5276
5401
|
* // }
|
|
5277
5402
|
* ```
|
|
5278
5403
|
*
|
|
5404
|
+
* @see {@link timeoutFail} for a version that raises a custom error.
|
|
5405
|
+
* @see {@link timeoutFailCause} for a version that raises a custom defect.
|
|
5406
|
+
* @see {@link timeoutTo} for a version that allows specifying both success and
|
|
5407
|
+
* timeout handlers.
|
|
5408
|
+
*
|
|
5279
5409
|
* @since 2.0.0
|
|
5280
5410
|
* @category Delays & Timeouts
|
|
5281
5411
|
*/
|
|
@@ -5299,13 +5429,8 @@ export const timeout = circular.timeout;
|
|
|
5299
5429
|
* to fail, making it easier to manage situations where you expect tasks might
|
|
5300
5430
|
* take too long but want to continue executing other tasks.
|
|
5301
5431
|
*
|
|
5302
|
-
*
|
|
5303
|
-
* @see {@link timeoutFail} for a version that raises a custom error.
|
|
5304
|
-
* @see {@link timeoutFailCause} for a version that raises a custom defect.
|
|
5305
|
-
* @see {@link timeoutTo} for a version that allows specifying both success and
|
|
5306
|
-
* timeout handlers.
|
|
5432
|
+
* **Example**
|
|
5307
5433
|
*
|
|
5308
|
-
* @example
|
|
5309
5434
|
* ```ts
|
|
5310
5435
|
* import { Effect } from "effect"
|
|
5311
5436
|
*
|
|
@@ -5321,7 +5446,7 @@ export const timeout = circular.timeout;
|
|
|
5321
5446
|
* task.pipe(Effect.timeoutOption("1 second"))
|
|
5322
5447
|
* ])
|
|
5323
5448
|
*
|
|
5324
|
-
*
|
|
5449
|
+
* Effect.runPromise(timedOutEffect).then(console.log)
|
|
5325
5450
|
* // Output:
|
|
5326
5451
|
* // Start processing...
|
|
5327
5452
|
* // Processing complete.
|
|
@@ -5332,6 +5457,12 @@ export const timeout = circular.timeout;
|
|
|
5332
5457
|
* // ]
|
|
5333
5458
|
* ```
|
|
5334
5459
|
*
|
|
5460
|
+
* @see {@link timeout} for a version that raises a `TimeoutException`.
|
|
5461
|
+
* @see {@link timeoutFail} for a version that raises a custom error.
|
|
5462
|
+
* @see {@link timeoutFailCause} for a version that raises a custom defect.
|
|
5463
|
+
* @see {@link timeoutTo} for a version that allows specifying both success and
|
|
5464
|
+
* timeout handlers.
|
|
5465
|
+
*
|
|
5335
5466
|
* @since 3.1.0
|
|
5336
5467
|
* @category Delays & Timeouts
|
|
5337
5468
|
*/
|
|
@@ -5356,12 +5487,8 @@ export const timeoutOption = circular.timeoutOption;
|
|
|
5356
5487
|
* normally. Otherwise, the `onTimeout` function is triggered, and its output is
|
|
5357
5488
|
* used as the error for the effect.
|
|
5358
5489
|
*
|
|
5359
|
-
*
|
|
5360
|
-
* @see {@link timeoutFailCause} for a version that raises a custom defect.
|
|
5361
|
-
* @see {@link timeoutTo} for a version that allows specifying both success and
|
|
5362
|
-
* timeout handlers.
|
|
5490
|
+
* **Example**
|
|
5363
5491
|
*
|
|
5364
|
-
* @example
|
|
5365
5492
|
* ```ts
|
|
5366
5493
|
* import { Effect } from "effect"
|
|
5367
5494
|
*
|
|
@@ -5383,7 +5510,7 @@ export const timeoutOption = circular.timeoutOption;
|
|
|
5383
5510
|
* })
|
|
5384
5511
|
* )
|
|
5385
5512
|
*
|
|
5386
|
-
*
|
|
5513
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
5387
5514
|
* // Output:
|
|
5388
5515
|
* // Start processing...
|
|
5389
5516
|
* // {
|
|
@@ -5397,6 +5524,11 @@ export const timeoutOption = circular.timeoutOption;
|
|
|
5397
5524
|
* // }
|
|
5398
5525
|
* ```
|
|
5399
5526
|
*
|
|
5527
|
+
* @see {@link timeout} for a version that raises a `TimeoutException`.
|
|
5528
|
+
* @see {@link timeoutFailCause} for a version that raises a custom defect.
|
|
5529
|
+
* @see {@link timeoutTo} for a version that allows specifying both success and
|
|
5530
|
+
* timeout handlers.
|
|
5531
|
+
*
|
|
5400
5532
|
* @since 2.0.0
|
|
5401
5533
|
* @category Delays & Timeouts
|
|
5402
5534
|
*/
|
|
@@ -5423,12 +5555,8 @@ export const timeoutFail = circular.timeoutFail;
|
|
|
5423
5555
|
* failures in your application and wish to include meaningful information in
|
|
5424
5556
|
* the defect.
|
|
5425
5557
|
*
|
|
5426
|
-
*
|
|
5427
|
-
* @see {@link timeoutFail} for a version that raises a custom error.
|
|
5428
|
-
* @see {@link timeoutTo} for a version that allows specifying both success and
|
|
5429
|
-
* timeout handlers.
|
|
5558
|
+
* **Example**
|
|
5430
5559
|
*
|
|
5431
|
-
* @example
|
|
5432
5560
|
* ```ts
|
|
5433
5561
|
* import { Effect, Cause } from "effect"
|
|
5434
5562
|
*
|
|
@@ -5446,7 +5574,7 @@ export const timeoutFail = circular.timeoutFail;
|
|
|
5446
5574
|
* })
|
|
5447
5575
|
* )
|
|
5448
5576
|
*
|
|
5449
|
-
*
|
|
5577
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
5450
5578
|
* // Output:
|
|
5451
5579
|
* // Start processing...
|
|
5452
5580
|
* // {
|
|
@@ -5456,6 +5584,11 @@ export const timeoutFail = circular.timeoutFail;
|
|
|
5456
5584
|
* // }
|
|
5457
5585
|
* ```
|
|
5458
5586
|
*
|
|
5587
|
+
* @see {@link timeout} for a version that raises a `TimeoutException`.
|
|
5588
|
+
* @see {@link timeoutFail} for a version that raises a custom error.
|
|
5589
|
+
* @see {@link timeoutTo} for a version that allows specifying both success and
|
|
5590
|
+
* timeout handlers.
|
|
5591
|
+
*
|
|
5459
5592
|
* @since 2.0.0
|
|
5460
5593
|
* @category Delays & Timeouts
|
|
5461
5594
|
*/
|
|
@@ -5481,11 +5614,8 @@ export const timeoutFailCause = circular.timeoutFailCause;
|
|
|
5481
5614
|
* and successes into a specific data structure, like an `Either` type, to
|
|
5482
5615
|
* represent these outcomes in a meaningful way.
|
|
5483
5616
|
*
|
|
5484
|
-
*
|
|
5485
|
-
* @see {@link timeoutFail} for a version that raises a custom error.
|
|
5486
|
-
* @see {@link timeoutFailCause} for a version that raises a custom defect.
|
|
5617
|
+
* **Example**
|
|
5487
5618
|
*
|
|
5488
|
-
* @example
|
|
5489
5619
|
* ```ts
|
|
5490
5620
|
* import { Effect, Either } from "effect"
|
|
5491
5621
|
*
|
|
@@ -5506,7 +5636,7 @@ export const timeoutFailCause = circular.timeoutFailCause;
|
|
|
5506
5636
|
* })
|
|
5507
5637
|
* )
|
|
5508
5638
|
*
|
|
5509
|
-
*
|
|
5639
|
+
* Effect.runPromise(program).then(console.log)
|
|
5510
5640
|
* // Output:
|
|
5511
5641
|
* // Start processing...
|
|
5512
5642
|
* // {
|
|
@@ -5516,6 +5646,10 @@ export const timeoutFailCause = circular.timeoutFailCause;
|
|
|
5516
5646
|
* // }
|
|
5517
5647
|
* ```
|
|
5518
5648
|
*
|
|
5649
|
+
* @see {@link timeout} for a version that raises a `TimeoutException`.
|
|
5650
|
+
* @see {@link timeoutFail} for a version that raises a custom error.
|
|
5651
|
+
* @see {@link timeoutFailCause} for a version that raises a custom defect.
|
|
5652
|
+
*
|
|
5519
5653
|
* @since 2.0.0
|
|
5520
5654
|
* @category Delays & Timeouts
|
|
5521
5655
|
*/
|
|
@@ -5548,7 +5682,8 @@ export const configProviderWith = defaultServices.configProviderWith;
|
|
|
5548
5682
|
* This is particularly useful when you need to use a different set of
|
|
5549
5683
|
* configuration values or sources for specific parts of your application.
|
|
5550
5684
|
*
|
|
5551
|
-
*
|
|
5685
|
+
* **Example**
|
|
5686
|
+
*
|
|
5552
5687
|
* ```ts
|
|
5553
5688
|
* import { Config, ConfigProvider, Effect } from "effect"
|
|
5554
5689
|
*
|
|
@@ -5563,7 +5698,7 @@ export const configProviderWith = defaultServices.configProviderWith;
|
|
|
5563
5698
|
* })
|
|
5564
5699
|
* )
|
|
5565
5700
|
*
|
|
5566
|
-
*
|
|
5701
|
+
* Effect.runPromise(program)
|
|
5567
5702
|
* // Output:
|
|
5568
5703
|
* // Config value: custom-value
|
|
5569
5704
|
* ```
|
|
@@ -5636,7 +5771,8 @@ export const contextWithEffect = core.contextWithEffect;
|
|
|
5636
5771
|
* This function allows you to transform the context required by an effect,
|
|
5637
5772
|
* providing part of the context and leaving the rest to be fulfilled later.
|
|
5638
5773
|
*
|
|
5639
|
-
*
|
|
5774
|
+
* **Example**
|
|
5775
|
+
*
|
|
5640
5776
|
* ```ts
|
|
5641
5777
|
* import { Context, Effect } from "effect"
|
|
5642
5778
|
*
|
|
@@ -5688,9 +5824,8 @@ export const mapInputContext = core.mapInputContext;
|
|
|
5688
5824
|
* environment for effects. For example, layers can be used to configure
|
|
5689
5825
|
* databases, logging services, or any other required dependencies.
|
|
5690
5826
|
*
|
|
5691
|
-
*
|
|
5827
|
+
* **Example**
|
|
5692
5828
|
*
|
|
5693
|
-
* @example
|
|
5694
5829
|
* ```ts
|
|
5695
5830
|
* import { Context, Effect, Layer } from "effect"
|
|
5696
5831
|
*
|
|
@@ -5719,12 +5854,14 @@ export const mapInputContext = core.mapInputContext;
|
|
|
5719
5854
|
* // ▼
|
|
5720
5855
|
* const runnable = Effect.provide(program, DatabaseLive)
|
|
5721
5856
|
*
|
|
5722
|
-
*
|
|
5857
|
+
* Effect.runPromise(runnable).then(console.log)
|
|
5723
5858
|
* // Output:
|
|
5724
5859
|
* // timestamp=... level=INFO fiber=#0 message="Executing query: SELECT * FROM users"
|
|
5725
5860
|
* // []
|
|
5726
5861
|
* ```
|
|
5727
5862
|
*
|
|
5863
|
+
* @see {@link provideService} for providing a service to an effect.
|
|
5864
|
+
*
|
|
5728
5865
|
* @since 2.0.0
|
|
5729
5866
|
* @category Context
|
|
5730
5867
|
*/
|
|
@@ -5745,9 +5882,8 @@ export const provide = layer.effect_provide;
|
|
|
5745
5882
|
* provided, all parts of the effect that rely on the service will automatically
|
|
5746
5883
|
* use the implementation you supplied.
|
|
5747
5884
|
*
|
|
5748
|
-
*
|
|
5885
|
+
* **Example**
|
|
5749
5886
|
*
|
|
5750
|
-
* @example
|
|
5751
5887
|
* ```ts
|
|
5752
5888
|
* import { Effect, Context } from "effect"
|
|
5753
5889
|
*
|
|
@@ -5773,11 +5909,13 @@ export const provide = layer.effect_provide;
|
|
|
5773
5909
|
* })
|
|
5774
5910
|
*
|
|
5775
5911
|
* // Run successfully
|
|
5776
|
-
*
|
|
5912
|
+
* Effect.runPromise(runnable)
|
|
5777
5913
|
* // Example Output:
|
|
5778
5914
|
* // random number: 0.8241872233134417
|
|
5779
5915
|
* ```
|
|
5780
5916
|
*
|
|
5917
|
+
* @see {@link provide} for providing multiple layers to an effect.
|
|
5918
|
+
*
|
|
5781
5919
|
* @since 2.0.0
|
|
5782
5920
|
* @category Context
|
|
5783
5921
|
*/
|
|
@@ -5904,11 +6042,8 @@ export const updateService = effect.updateService;
|
|
|
5904
6042
|
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
5905
6043
|
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
5906
6044
|
*
|
|
5907
|
-
*
|
|
5908
|
-
* @see {@link bindTo}
|
|
5909
|
-
* @see {@link let_ let}
|
|
6045
|
+
* **Example**
|
|
5910
6046
|
*
|
|
5911
|
-
* @example
|
|
5912
6047
|
* ```ts
|
|
5913
6048
|
* import * as assert from "node:assert"
|
|
5914
6049
|
* import { Effect, pipe } from "effect"
|
|
@@ -5922,6 +6057,10 @@ export const updateService = effect.updateService;
|
|
|
5922
6057
|
* assert.deepStrictEqual(Effect.runSync(result), { x: 2, y: 3, sum: 5 })
|
|
5923
6058
|
* ```
|
|
5924
6059
|
*
|
|
6060
|
+
* @see {@link bind}
|
|
6061
|
+
* @see {@link bindTo}
|
|
6062
|
+
* @see {@link let_ let}
|
|
6063
|
+
*
|
|
5925
6064
|
* @category Do notation
|
|
5926
6065
|
* @since 2.0.0
|
|
5927
6066
|
*/
|
|
@@ -5936,11 +6075,8 @@ export const Do = effect.Do;
|
|
|
5936
6075
|
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
5937
6076
|
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
5938
6077
|
*
|
|
5939
|
-
*
|
|
5940
|
-
* @see {@link bindTo}
|
|
5941
|
-
* @see {@link let_ let}
|
|
6078
|
+
* **Example**
|
|
5942
6079
|
*
|
|
5943
|
-
* @example
|
|
5944
6080
|
* ```ts
|
|
5945
6081
|
* import * as assert from "node:assert"
|
|
5946
6082
|
* import { Effect, pipe } from "effect"
|
|
@@ -5954,6 +6090,10 @@ export const Do = effect.Do;
|
|
|
5954
6090
|
* assert.deepStrictEqual(Effect.runSync(result), { x: 2, y: 3, sum: 5 })
|
|
5955
6091
|
* ```
|
|
5956
6092
|
*
|
|
6093
|
+
* @see {@link Do}
|
|
6094
|
+
* @see {@link bindTo}
|
|
6095
|
+
* @see {@link let_ let}
|
|
6096
|
+
*
|
|
5957
6097
|
* @category Do notation
|
|
5958
6098
|
* @since 2.0.0
|
|
5959
6099
|
*/
|
|
@@ -5963,7 +6103,8 @@ export const bind = effect.bind;
|
|
|
5963
6103
|
* when you want to concurrently run multiple effects and then combine their
|
|
5964
6104
|
* results in a Do notation pipeline.
|
|
5965
6105
|
*
|
|
5966
|
-
*
|
|
6106
|
+
* **Example**
|
|
6107
|
+
*
|
|
5967
6108
|
* ```ts
|
|
5968
6109
|
* import * as assert from "node:assert"
|
|
5969
6110
|
* import { Effect, Either, pipe } from "effect"
|
|
@@ -5993,11 +6134,8 @@ export const bindAll = circular.bindAll;
|
|
|
5993
6134
|
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
5994
6135
|
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
5995
6136
|
*
|
|
5996
|
-
*
|
|
5997
|
-
* @see {@link bind}
|
|
5998
|
-
* @see {@link let_ let}
|
|
6137
|
+
* **Example**
|
|
5999
6138
|
*
|
|
6000
|
-
* @example
|
|
6001
6139
|
* ```ts
|
|
6002
6140
|
* import * as assert from "node:assert"
|
|
6003
6141
|
* import { Effect, pipe } from "effect"
|
|
@@ -6011,6 +6149,10 @@ export const bindAll = circular.bindAll;
|
|
|
6011
6149
|
* assert.deepStrictEqual(Effect.runSync(result), { x: 2, y: 3, sum: 5 })
|
|
6012
6150
|
* ```
|
|
6013
6151
|
*
|
|
6152
|
+
* @see {@link Do}
|
|
6153
|
+
* @see {@link bind}
|
|
6154
|
+
* @see {@link let_ let}
|
|
6155
|
+
*
|
|
6014
6156
|
* @category Do notation
|
|
6015
6157
|
* @since 2.0.0
|
|
6016
6158
|
*/
|
|
@@ -6027,11 +6169,8 @@ export {
|
|
|
6027
6169
|
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
6028
6170
|
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
6029
6171
|
*
|
|
6030
|
-
*
|
|
6031
|
-
* @see {@link bind}
|
|
6032
|
-
* @see {@link bindTo}
|
|
6172
|
+
* **Example**
|
|
6033
6173
|
*
|
|
6034
|
-
* @example
|
|
6035
6174
|
* ```ts
|
|
6036
6175
|
* import * as assert from "node:assert"
|
|
6037
6176
|
* import { Effect, pipe } from "effect"
|
|
@@ -6045,6 +6184,11 @@ export {
|
|
|
6045
6184
|
* assert.deepStrictEqual(Effect.runSync(result), { x: 2, y: 3, sum: 5 })
|
|
6046
6185
|
*
|
|
6047
6186
|
* ```
|
|
6187
|
+
*
|
|
6188
|
+
* @see {@link Do}
|
|
6189
|
+
* @see {@link bind}
|
|
6190
|
+
* @see {@link bindTo}
|
|
6191
|
+
*
|
|
6048
6192
|
* @category Do notation
|
|
6049
6193
|
* @since 2.0.0
|
|
6050
6194
|
*/
|
|
@@ -6064,17 +6208,14 @@ let_ as let };
|
|
|
6064
6208
|
* directly. However, unrecoverable errors, also referred to as defects, are
|
|
6065
6209
|
* not captured and will still result in failure.
|
|
6066
6210
|
*
|
|
6067
|
-
*
|
|
6068
|
-
* @see {@link exit} for a version that encapsulates both recoverable errors and defects in an `Exit`.
|
|
6211
|
+
* **Example** (Using Effect.option to Handle Errors)
|
|
6069
6212
|
*
|
|
6070
|
-
* @example
|
|
6071
6213
|
* ```ts
|
|
6072
|
-
* // Title: Using Effect.option to Handle Errors
|
|
6073
6214
|
* import { Effect } from "effect"
|
|
6074
6215
|
*
|
|
6075
6216
|
* const maybe1 = Effect.option(Effect.succeed(1))
|
|
6076
6217
|
*
|
|
6077
|
-
*
|
|
6218
|
+
* Effect.runPromiseExit(maybe1).then(console.log)
|
|
6078
6219
|
* // Output:
|
|
6079
6220
|
* // {
|
|
6080
6221
|
* // _id: 'Exit',
|
|
@@ -6084,7 +6225,7 @@ let_ as let };
|
|
|
6084
6225
|
*
|
|
6085
6226
|
* const maybe2 = Effect.option(Effect.fail("Uh oh!"))
|
|
6086
6227
|
*
|
|
6087
|
-
*
|
|
6228
|
+
* Effect.runPromiseExit(maybe2).then(console.log)
|
|
6088
6229
|
* // Output:
|
|
6089
6230
|
* // {
|
|
6090
6231
|
* // _id: 'Exit',
|
|
@@ -6094,7 +6235,7 @@ let_ as let };
|
|
|
6094
6235
|
*
|
|
6095
6236
|
* const maybe3 = Effect.option(Effect.die("Boom!"))
|
|
6096
6237
|
*
|
|
6097
|
-
*
|
|
6238
|
+
* Effect.runPromiseExit(maybe3).then(console.log)
|
|
6098
6239
|
* // Output:
|
|
6099
6240
|
* // {
|
|
6100
6241
|
* // _id: 'Exit',
|
|
@@ -6103,6 +6244,9 @@ let_ as let };
|
|
|
6103
6244
|
* // }
|
|
6104
6245
|
* ```
|
|
6105
6246
|
*
|
|
6247
|
+
* @see {@link either} for a version that uses `Either` instead.
|
|
6248
|
+
* @see {@link exit} for a version that encapsulates both recoverable errors and defects in an `Exit`.
|
|
6249
|
+
*
|
|
6106
6250
|
* @since 2.0.0
|
|
6107
6251
|
* @category Outcome Encapsulation
|
|
6108
6252
|
*/
|
|
@@ -6130,10 +6274,8 @@ export const option = effect.option;
|
|
|
6130
6274
|
* The resulting effect cannot fail directly because all recoverable failures
|
|
6131
6275
|
* are represented inside the `Either` type.
|
|
6132
6276
|
*
|
|
6133
|
-
*
|
|
6134
|
-
* @see {@link exit} for a version that encapsulates both recoverable errors and defects in an `Exit`.
|
|
6277
|
+
* **Example**
|
|
6135
6278
|
*
|
|
6136
|
-
* @example
|
|
6137
6279
|
* ```ts
|
|
6138
6280
|
* import { Effect, Either, Random } from "effect"
|
|
6139
6281
|
*
|
|
@@ -6172,6 +6314,9 @@ export const option = effect.option;
|
|
|
6172
6314
|
* })
|
|
6173
6315
|
* ```
|
|
6174
6316
|
*
|
|
6317
|
+
* @see {@link option} for a version that uses `Option` instead.
|
|
6318
|
+
* @see {@link exit} for a version that encapsulates both recoverable errors and defects in an `Exit`.
|
|
6319
|
+
*
|
|
6175
6320
|
* @since 2.0.0
|
|
6176
6321
|
* @category Outcome Encapsulation
|
|
6177
6322
|
*/
|
|
@@ -6195,10 +6340,8 @@ export const either = core.either;
|
|
|
6195
6340
|
* error type is `never`). It is particularly useful for workflows where all
|
|
6196
6341
|
* outcomes, including unexpected defects, must be managed and analyzed.
|
|
6197
6342
|
*
|
|
6198
|
-
*
|
|
6199
|
-
* @see {@link either} for a version that uses `Either` instead.
|
|
6343
|
+
* **Example**
|
|
6200
6344
|
*
|
|
6201
|
-
* @example
|
|
6202
6345
|
* ```ts
|
|
6203
6346
|
* import { Effect, Cause, Console, Exit } from "effect"
|
|
6204
6347
|
*
|
|
@@ -6223,7 +6366,7 @@ export const either = core.either;
|
|
|
6223
6366
|
* })
|
|
6224
6367
|
*
|
|
6225
6368
|
* // We get an Exit.Success because we caught all failures
|
|
6226
|
-
*
|
|
6369
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
6227
6370
|
* // Output:
|
|
6228
6371
|
* // RuntimeException defect caught: Boom!
|
|
6229
6372
|
* // {
|
|
@@ -6233,6 +6376,9 @@ export const either = core.either;
|
|
|
6233
6376
|
* // }
|
|
6234
6377
|
* ```
|
|
6235
6378
|
*
|
|
6379
|
+
* @see {@link option} for a version that uses `Option` instead.
|
|
6380
|
+
* @see {@link either} for a version that uses `Either` instead.
|
|
6381
|
+
*
|
|
6236
6382
|
* @since 2.0.0
|
|
6237
6383
|
* @category Outcome Encapsulation
|
|
6238
6384
|
*/
|
|
@@ -6247,7 +6393,8 @@ export const exit = core.exit;
|
|
|
6247
6393
|
* completed with the success value. If the effect fails, the `Deferred` is completed with the
|
|
6248
6394
|
* failure. Additionally, if the effect is interrupted, the `Deferred` will also be interrupted.
|
|
6249
6395
|
*
|
|
6250
|
-
*
|
|
6396
|
+
* **Example**
|
|
6397
|
+
*
|
|
6251
6398
|
* ```ts
|
|
6252
6399
|
* import { Deferred, Effect } from "effect"
|
|
6253
6400
|
*
|
|
@@ -6268,7 +6415,7 @@ export const exit = core.exit;
|
|
|
6268
6415
|
* return isCompleted
|
|
6269
6416
|
* })
|
|
6270
6417
|
*
|
|
6271
|
-
*
|
|
6418
|
+
* Effect.runPromise(program).then(console.log)
|
|
6272
6419
|
* // Output:
|
|
6273
6420
|
* // 42
|
|
6274
6421
|
* // true
|
|
@@ -6287,9 +6434,9 @@ export {
|
|
|
6287
6434
|
* evaluates to `true` or `false`. If the predicate is `true`, the `onTrue` effect
|
|
6288
6435
|
* is executed. If it is `false`, the `onFalse` effect is executed instead.
|
|
6289
6436
|
*
|
|
6290
|
-
*
|
|
6437
|
+
* **Example** (Simulating a Coin Flip)
|
|
6438
|
+
*
|
|
6291
6439
|
* ```ts
|
|
6292
|
-
* // Title: Simulating a Coin Flip
|
|
6293
6440
|
* import { Effect, Random, Console } from "effect"
|
|
6294
6441
|
*
|
|
6295
6442
|
* const flipTheCoin = Effect.if(Random.nextBoolean, {
|
|
@@ -6297,9 +6444,9 @@ export {
|
|
|
6297
6444
|
* onFalse: () => Console.log("Tail") // Runs if the predicate is false
|
|
6298
6445
|
* })
|
|
6299
6446
|
*
|
|
6300
|
-
*
|
|
6301
|
-
*
|
|
6447
|
+
* Effect.runFork(flipTheCoin)
|
|
6302
6448
|
* ```
|
|
6449
|
+
*
|
|
6303
6450
|
* @since 2.0.0
|
|
6304
6451
|
* @category Conditional Operators
|
|
6305
6452
|
*/
|
|
@@ -6371,7 +6518,8 @@ export const filterOrElse = effect.filterOrElse;
|
|
|
6371
6518
|
* guard](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates).
|
|
6372
6519
|
* Let's explore this concept through an example:
|
|
6373
6520
|
*
|
|
6374
|
-
*
|
|
6521
|
+
* **Example**
|
|
6522
|
+
*
|
|
6375
6523
|
* ```ts
|
|
6376
6524
|
* import { Effect, pipe } from "effect"
|
|
6377
6525
|
*
|
|
@@ -6410,7 +6558,8 @@ export const filterOrFail = effect.filterOrFail;
|
|
|
6410
6558
|
* effect. The `orElse` effect can produce an alternative value or perform
|
|
6411
6559
|
* additional computations.
|
|
6412
6560
|
*
|
|
6413
|
-
*
|
|
6561
|
+
* **Example**
|
|
6562
|
+
*
|
|
6414
6563
|
* ```ts
|
|
6415
6564
|
* import { Effect, pipe } from "effect"
|
|
6416
6565
|
*
|
|
@@ -6450,7 +6599,8 @@ export const filterEffectOrElse = core.filterEffectOrElse;
|
|
|
6450
6599
|
* This is useful for enforcing constraints and treating violations as
|
|
6451
6600
|
* recoverable errors.
|
|
6452
6601
|
*
|
|
6453
|
-
*
|
|
6602
|
+
* **Example**
|
|
6603
|
+
*
|
|
6454
6604
|
* ```ts
|
|
6455
6605
|
* import { Effect, pipe } from "effect"
|
|
6456
6606
|
*
|
|
@@ -6512,12 +6662,9 @@ export const unlessEffect = effect.unlessEffect;
|
|
|
6512
6662
|
* whether to execute an effect based on runtime logic, while also representing
|
|
6513
6663
|
* the skipped case explicitly.
|
|
6514
6664
|
*
|
|
6515
|
-
*
|
|
6516
|
-
* @see {@link unless} for a version that executes the effect when the condition is `false`.
|
|
6665
|
+
* **Example** (Conditional Effect Execution)
|
|
6517
6666
|
*
|
|
6518
|
-
* @example
|
|
6519
6667
|
* ```ts
|
|
6520
|
-
* // Title: Conditional Effect Execution
|
|
6521
6668
|
* import { Effect, Option } from "effect"
|
|
6522
6669
|
*
|
|
6523
6670
|
* const validateWeightOption = (
|
|
@@ -6527,7 +6674,7 @@ export const unlessEffect = effect.unlessEffect;
|
|
|
6527
6674
|
* Effect.succeed(weight).pipe(Effect.when(() => weight >= 0))
|
|
6528
6675
|
*
|
|
6529
6676
|
* // Run with a valid weight
|
|
6530
|
-
*
|
|
6677
|
+
* Effect.runPromise(validateWeightOption(100)).then(console.log)
|
|
6531
6678
|
* // Output:
|
|
6532
6679
|
* // {
|
|
6533
6680
|
* // _id: "Option",
|
|
@@ -6536,7 +6683,7 @@ export const unlessEffect = effect.unlessEffect;
|
|
|
6536
6683
|
* // }
|
|
6537
6684
|
*
|
|
6538
6685
|
* // Run with an invalid weight
|
|
6539
|
-
*
|
|
6686
|
+
* Effect.runPromise(validateWeightOption(-5)).then(console.log)
|
|
6540
6687
|
* // Output:
|
|
6541
6688
|
* // {
|
|
6542
6689
|
* // _id: "Option",
|
|
@@ -6544,6 +6691,9 @@ export const unlessEffect = effect.unlessEffect;
|
|
|
6544
6691
|
* // }
|
|
6545
6692
|
* ```
|
|
6546
6693
|
*
|
|
6694
|
+
* @see {@link whenEffect} for a version that allows the condition to be an effect.
|
|
6695
|
+
* @see {@link unless} for a version that executes the effect when the condition is `false`.
|
|
6696
|
+
*
|
|
6547
6697
|
* @since 2.0.0
|
|
6548
6698
|
* @category Conditional Operators
|
|
6549
6699
|
*/
|
|
@@ -6565,12 +6715,9 @@ export const when = effect.when;
|
|
|
6565
6715
|
* depends on the result of another effect, such as a random value, a
|
|
6566
6716
|
* user-provided input, or a network request result.
|
|
6567
6717
|
*
|
|
6568
|
-
*
|
|
6569
|
-
* @see {@link unlessEffect} for a version that executes the effect when the condition is `false`.
|
|
6718
|
+
* **Example** (Using an Effect as a Condition)
|
|
6570
6719
|
*
|
|
6571
|
-
* @example
|
|
6572
6720
|
* ```ts
|
|
6573
|
-
* // Title: Using an Effect as a Condition
|
|
6574
6721
|
* import { Effect, Random } from "effect"
|
|
6575
6722
|
*
|
|
6576
6723
|
* const randomIntOption = Random.nextInt.pipe(
|
|
@@ -6582,6 +6729,9 @@ export const when = effect.when;
|
|
|
6582
6729
|
* // { _id: 'Option', _tag: 'Some', value: 8609104974198840 }
|
|
6583
6730
|
* ```
|
|
6584
6731
|
*
|
|
6732
|
+
* @see {@link when} for a version that allows the condition to be a boolean.
|
|
6733
|
+
* @see {@link unlessEffect} for a version that executes the effect when the condition is `false`.
|
|
6734
|
+
*
|
|
6585
6735
|
* @since 2.0.0
|
|
6586
6736
|
* @category Conditional Operators
|
|
6587
6737
|
*/
|
|
@@ -6650,9 +6800,8 @@ export const whenRef = effect.whenRef;
|
|
|
6650
6800
|
* step produces a new `Effect` while flattening any nested effects that may
|
|
6651
6801
|
* occur.
|
|
6652
6802
|
*
|
|
6653
|
-
*
|
|
6803
|
+
* **Example**
|
|
6654
6804
|
*
|
|
6655
|
-
* @example
|
|
6656
6805
|
* ```ts
|
|
6657
6806
|
* import { pipe, Effect } from "effect"
|
|
6658
6807
|
*
|
|
@@ -6674,10 +6823,12 @@ export const whenRef = effect.whenRef;
|
|
|
6674
6823
|
* Effect.flatMap((amount) => applyDiscount(amount, 5))
|
|
6675
6824
|
* )
|
|
6676
6825
|
*
|
|
6677
|
-
*
|
|
6826
|
+
* Effect.runPromise(finalAmount).then(console.log)
|
|
6678
6827
|
* // Output: 95
|
|
6679
6828
|
* ```
|
|
6680
6829
|
*
|
|
6830
|
+
* @see {@link tap} for a version that ignores the result of the effect.
|
|
6831
|
+
*
|
|
6681
6832
|
* @since 2.0.0
|
|
6682
6833
|
* @category Sequencing
|
|
6683
6834
|
*/
|
|
@@ -6716,9 +6867,9 @@ export const flatMap = core.flatMap;
|
|
|
6716
6867
|
* **Note:** `andThen` works well with both `Option` and `Either` types,
|
|
6717
6868
|
* treating them as effects.
|
|
6718
6869
|
*
|
|
6719
|
-
*
|
|
6870
|
+
* **Example** (Applying a Discount Based on Fetched Amount)
|
|
6871
|
+
*
|
|
6720
6872
|
* ```ts
|
|
6721
|
-
* // Title: Applying a Discount Based on Fetched Amount
|
|
6722
6873
|
* import { pipe, Effect } from "effect"
|
|
6723
6874
|
*
|
|
6724
6875
|
* // Function to apply a discount safely to a transaction amount
|
|
@@ -6740,7 +6891,7 @@ export const flatMap = core.flatMap;
|
|
|
6740
6891
|
* Effect.flatMap((amount) => applyDiscount(amount, 5))
|
|
6741
6892
|
* )
|
|
6742
6893
|
*
|
|
6743
|
-
*
|
|
6894
|
+
* Effect.runPromise(result1).then(console.log)
|
|
6744
6895
|
* // Output: 190
|
|
6745
6896
|
*
|
|
6746
6897
|
* // Using Effect.andThen
|
|
@@ -6750,7 +6901,7 @@ export const flatMap = core.flatMap;
|
|
|
6750
6901
|
* Effect.andThen((amount) => applyDiscount(amount, 5))
|
|
6751
6902
|
* )
|
|
6752
6903
|
*
|
|
6753
|
-
*
|
|
6904
|
+
* Effect.runPromise(result2).then(console.log)
|
|
6754
6905
|
* // Output: 190
|
|
6755
6906
|
* ```
|
|
6756
6907
|
*
|
|
@@ -6789,12 +6940,9 @@ export const flatten = core.flatten;
|
|
|
6789
6940
|
* wraps the result in an `Either` type, allowing you to see if the result
|
|
6790
6941
|
* was a success (`Right`) or a failure (`Left`).
|
|
6791
6942
|
*
|
|
6792
|
-
*
|
|
6793
|
-
* @see {@link raceFirst} for a version that returns the result of the first effect to complete.
|
|
6943
|
+
* **Example** (Both Tasks Succeed)
|
|
6794
6944
|
*
|
|
6795
|
-
* @example
|
|
6796
6945
|
* ```ts
|
|
6797
|
-
* // Title: Both Tasks Succeed
|
|
6798
6946
|
* import { Effect, Console } from "effect"
|
|
6799
6947
|
*
|
|
6800
6948
|
* const task1 = Effect.succeed("task1").pipe(
|
|
@@ -6810,15 +6958,15 @@ export const flatten = core.flatten;
|
|
|
6810
6958
|
*
|
|
6811
6959
|
* const program = Effect.race(task1, task2)
|
|
6812
6960
|
*
|
|
6813
|
-
*
|
|
6961
|
+
* Effect.runFork(program)
|
|
6814
6962
|
* // Output:
|
|
6815
6963
|
* // task1 done
|
|
6816
6964
|
* // task2 interrupted
|
|
6817
6965
|
* ```
|
|
6818
6966
|
*
|
|
6819
|
-
*
|
|
6967
|
+
* **Example** (One Task Fails, One Succeeds)
|
|
6968
|
+
*
|
|
6820
6969
|
* ```ts
|
|
6821
|
-
* // Title: One Task Fails, One Succeeds
|
|
6822
6970
|
* import { Effect, Console } from "effect"
|
|
6823
6971
|
*
|
|
6824
6972
|
* const task1 = Effect.fail("task1").pipe(
|
|
@@ -6834,14 +6982,14 @@ export const flatten = core.flatten;
|
|
|
6834
6982
|
*
|
|
6835
6983
|
* const program = Effect.race(task1, task2)
|
|
6836
6984
|
*
|
|
6837
|
-
*
|
|
6985
|
+
* Effect.runFork(program)
|
|
6838
6986
|
* // Output:
|
|
6839
6987
|
* // task2 done
|
|
6840
6988
|
* ```
|
|
6841
6989
|
*
|
|
6842
|
-
*
|
|
6990
|
+
* **Example** (Both Tasks Fail)
|
|
6991
|
+
*
|
|
6843
6992
|
* ```ts
|
|
6844
|
-
* // Title: Both Tasks Fail
|
|
6845
6993
|
* import { Effect, Console } from "effect"
|
|
6846
6994
|
*
|
|
6847
6995
|
* const task1 = Effect.fail("task1").pipe(
|
|
@@ -6857,7 +7005,7 @@ export const flatten = core.flatten;
|
|
|
6857
7005
|
*
|
|
6858
7006
|
* const program = Effect.race(task1, task2)
|
|
6859
7007
|
*
|
|
6860
|
-
*
|
|
7008
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
6861
7009
|
* // Output:
|
|
6862
7010
|
* // {
|
|
6863
7011
|
* // _id: 'Exit',
|
|
@@ -6871,9 +7019,9 @@ export const flatten = core.flatten;
|
|
|
6871
7019
|
* // }
|
|
6872
7020
|
* ```
|
|
6873
7021
|
*
|
|
6874
|
-
*
|
|
7022
|
+
* **Example** (Handling Success or Failure with Either)
|
|
7023
|
+
*
|
|
6875
7024
|
* ```ts
|
|
6876
|
-
* // Title: Handling Success or Failure with Either
|
|
6877
7025
|
* import { Effect, Console } from "effect"
|
|
6878
7026
|
*
|
|
6879
7027
|
* const task1 = Effect.fail("task1").pipe(
|
|
@@ -6891,12 +7039,15 @@ export const flatten = core.flatten;
|
|
|
6891
7039
|
* // in Either to capture success or failure
|
|
6892
7040
|
* const program = Effect.race(Effect.either(task1), Effect.either(task2))
|
|
6893
7041
|
*
|
|
6894
|
-
*
|
|
7042
|
+
* Effect.runPromise(program).then(console.log)
|
|
6895
7043
|
* // Output:
|
|
6896
7044
|
* // task2 interrupted
|
|
6897
7045
|
* // { _id: 'Either', _tag: 'Left', left: 'task1' }
|
|
6898
7046
|
* ```
|
|
6899
7047
|
*
|
|
7048
|
+
* @see {@link raceAll} for a version that handles multiple effects.
|
|
7049
|
+
* @see {@link raceFirst} for a version that returns the result of the first effect to complete.
|
|
7050
|
+
*
|
|
6900
7051
|
* @since 2.0.0
|
|
6901
7052
|
* @category Racing
|
|
6902
7053
|
*/
|
|
@@ -6920,11 +7071,9 @@ export const race = fiberRuntime.race;
|
|
|
6920
7071
|
* retries, or when you want to optimize for the faster response without
|
|
6921
7072
|
* worrying about the other effects.
|
|
6922
7073
|
*
|
|
6923
|
-
*
|
|
7074
|
+
* **Example** (All Tasks Succeed)
|
|
6924
7075
|
*
|
|
6925
|
-
* @example
|
|
6926
7076
|
* ```ts
|
|
6927
|
-
* // Title: All Tasks Succeed
|
|
6928
7077
|
* import { Effect, Console } from "effect"
|
|
6929
7078
|
*
|
|
6930
7079
|
* const task1 = Effect.succeed("task1").pipe(
|
|
@@ -6946,16 +7095,16 @@ export const race = fiberRuntime.race;
|
|
|
6946
7095
|
*
|
|
6947
7096
|
* const program = Effect.raceAll([task1, task2, task3])
|
|
6948
7097
|
*
|
|
6949
|
-
*
|
|
7098
|
+
* Effect.runFork(program)
|
|
6950
7099
|
* // Output:
|
|
6951
7100
|
* // task1 done
|
|
6952
7101
|
* // task2 interrupted
|
|
6953
7102
|
* // task3 interrupted
|
|
6954
7103
|
* ```
|
|
6955
7104
|
*
|
|
6956
|
-
*
|
|
7105
|
+
* **Example** (One Task Fails, Two Tasks Succeed)
|
|
7106
|
+
*
|
|
6957
7107
|
* ```ts
|
|
6958
|
-
* // Title: One Task Fails, Two Tasks Succeed
|
|
6959
7108
|
* import { Effect, Console } from "effect"
|
|
6960
7109
|
*
|
|
6961
7110
|
* const task1 = Effect.fail("task1").pipe(
|
|
@@ -6977,15 +7126,15 @@ export const race = fiberRuntime.race;
|
|
|
6977
7126
|
*
|
|
6978
7127
|
* const program = Effect.raceAll([task1, task2, task3])
|
|
6979
7128
|
*
|
|
6980
|
-
*
|
|
7129
|
+
* Effect.runFork(program)
|
|
6981
7130
|
* // Output:
|
|
6982
7131
|
* // task3 done
|
|
6983
7132
|
* // task2 interrupted
|
|
6984
7133
|
* ```
|
|
6985
7134
|
*
|
|
6986
|
-
*
|
|
7135
|
+
* **Example** (All Tasks Fail)
|
|
7136
|
+
*
|
|
6987
7137
|
* ```ts
|
|
6988
|
-
* // Title: All Tasks Fail
|
|
6989
7138
|
* import { Effect, Console } from "effect"
|
|
6990
7139
|
*
|
|
6991
7140
|
* const task1 = Effect.fail("task1").pipe(
|
|
@@ -7007,7 +7156,7 @@ export const race = fiberRuntime.race;
|
|
|
7007
7156
|
*
|
|
7008
7157
|
* const program = Effect.raceAll([task1, task2, task3])
|
|
7009
7158
|
*
|
|
7010
|
-
*
|
|
7159
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
7011
7160
|
* // Output:
|
|
7012
7161
|
* // {
|
|
7013
7162
|
* // _id: 'Exit',
|
|
@@ -7016,6 +7165,8 @@ export const race = fiberRuntime.race;
|
|
|
7016
7165
|
* // }
|
|
7017
7166
|
* ```
|
|
7018
7167
|
*
|
|
7168
|
+
* @see {@link race} for a version that handles only two effects.
|
|
7169
|
+
*
|
|
7019
7170
|
* @since 2.0.0
|
|
7020
7171
|
* @category Racing
|
|
7021
7172
|
*/
|
|
@@ -7053,9 +7204,9 @@ export const raceAll = fiberRuntime.raceAll;
|
|
|
7053
7204
|
*
|
|
7054
7205
|
* This allows both effects to complete independently while still terminating the losing effect in the background.
|
|
7055
7206
|
*
|
|
7056
|
-
*
|
|
7207
|
+
* **Example** (Both Tasks Succeed)
|
|
7208
|
+
*
|
|
7057
7209
|
* ```ts
|
|
7058
|
-
* // Title: Both Tasks Succeed
|
|
7059
7210
|
* import { Effect, Console } from "effect"
|
|
7060
7211
|
*
|
|
7061
7212
|
* const task1 = Effect.succeed("task1").pipe(
|
|
@@ -7077,7 +7228,7 @@ export const raceAll = fiberRuntime.raceAll;
|
|
|
7077
7228
|
* Effect.tap(Console.log("more work..."))
|
|
7078
7229
|
* )
|
|
7079
7230
|
*
|
|
7080
|
-
*
|
|
7231
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
7081
7232
|
* // Output:
|
|
7082
7233
|
* // task1 done
|
|
7083
7234
|
* // task2 interrupted
|
|
@@ -7085,9 +7236,9 @@ export const raceAll = fiberRuntime.raceAll;
|
|
|
7085
7236
|
* // { _id: 'Exit', _tag: 'Success', value: 'task1' }
|
|
7086
7237
|
* ```
|
|
7087
7238
|
*
|
|
7088
|
-
*
|
|
7239
|
+
* **Example** (One Task Fails, One Succeeds)
|
|
7240
|
+
*
|
|
7089
7241
|
* ```ts
|
|
7090
|
-
* // Title: One Task Fails, One Succeeds
|
|
7091
7242
|
* import { Effect, Console } from "effect"
|
|
7092
7243
|
*
|
|
7093
7244
|
* const task1 = Effect.fail("task1").pipe(
|
|
@@ -7109,7 +7260,7 @@ export const raceAll = fiberRuntime.raceAll;
|
|
|
7109
7260
|
* Effect.tap(Console.log("more work..."))
|
|
7110
7261
|
* )
|
|
7111
7262
|
*
|
|
7112
|
-
*
|
|
7263
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
7113
7264
|
* // Output:
|
|
7114
7265
|
* // task2 interrupted
|
|
7115
7266
|
* // {
|
|
@@ -7119,9 +7270,9 @@ export const raceAll = fiberRuntime.raceAll;
|
|
|
7119
7270
|
* // }
|
|
7120
7271
|
* ```
|
|
7121
7272
|
*
|
|
7122
|
-
*
|
|
7273
|
+
* **Example** (Using Effect.disconnect for Quicker Return)
|
|
7274
|
+
*
|
|
7123
7275
|
* ```ts
|
|
7124
|
-
* // Title: Using Effect.disconnect for Quicker Return
|
|
7125
7276
|
* import { Effect, Console } from "effect"
|
|
7126
7277
|
*
|
|
7127
7278
|
* const task1 = Effect.succeed("task1").pipe(
|
|
@@ -7145,7 +7296,7 @@ export const raceAll = fiberRuntime.raceAll;
|
|
|
7145
7296
|
* Effect.disconnect(task2)
|
|
7146
7297
|
* ).pipe(Effect.tap(Console.log("more work...")))
|
|
7147
7298
|
*
|
|
7148
|
-
*
|
|
7299
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
7149
7300
|
* // Output:
|
|
7150
7301
|
* // task1 done
|
|
7151
7302
|
* // more work...
|
|
@@ -7178,9 +7329,9 @@ export const raceFirst = circular.raceFirst;
|
|
|
7178
7329
|
* effect without waiting for both to finish. It can be used whenever you want
|
|
7179
7330
|
* to take action based on the first available result.
|
|
7180
7331
|
*
|
|
7181
|
-
*
|
|
7332
|
+
* **Example** (Handling Results of Concurrent Tasks)
|
|
7333
|
+
*
|
|
7182
7334
|
* ```ts
|
|
7183
|
-
* // Title: Handling Results of Concurrent Tasks
|
|
7184
7335
|
* import { Effect, Console } from "effect"
|
|
7185
7336
|
*
|
|
7186
7337
|
* const task1 = Effect.succeed("task1").pipe(
|
|
@@ -7203,7 +7354,7 @@ export const raceFirst = circular.raceFirst;
|
|
|
7203
7354
|
* onOtherDone: (exit) => Console.log(`task2 exited with ${exit}`)
|
|
7204
7355
|
* })
|
|
7205
7356
|
*
|
|
7206
|
-
*
|
|
7357
|
+
* Effect.runFork(program)
|
|
7207
7358
|
* // Output:
|
|
7208
7359
|
* // task1 done
|
|
7209
7360
|
* // task1 exited with {
|
|
@@ -7245,11 +7396,9 @@ export const summarized = effect.summarized;
|
|
|
7245
7396
|
* observe or record an action but want the original value to be passed to the
|
|
7246
7397
|
* next step.
|
|
7247
7398
|
*
|
|
7248
|
-
*
|
|
7399
|
+
* **Example** (Logging a step in a pipeline)
|
|
7249
7400
|
*
|
|
7250
|
-
* @example
|
|
7251
7401
|
* ```ts
|
|
7252
|
-
* // Title: Logging a step in a pipeline
|
|
7253
7402
|
* import { Console, Effect, pipe } from "effect"
|
|
7254
7403
|
*
|
|
7255
7404
|
* // Function to apply a discount safely to a transaction amount
|
|
@@ -7272,12 +7421,14 @@ export const summarized = effect.summarized;
|
|
|
7272
7421
|
* Effect.flatMap((amount) => applyDiscount(amount, 5))
|
|
7273
7422
|
* )
|
|
7274
7423
|
*
|
|
7275
|
-
*
|
|
7424
|
+
* Effect.runPromise(finalAmount).then(console.log)
|
|
7276
7425
|
* // Output:
|
|
7277
7426
|
* // Apply a discount to: 100
|
|
7278
7427
|
* // 95
|
|
7279
7428
|
* ```
|
|
7280
7429
|
*
|
|
7430
|
+
* @see {@link flatMap} for a version that allows you to change the value.
|
|
7431
|
+
*
|
|
7281
7432
|
* @since 2.0.0
|
|
7282
7433
|
* @category Sequencing
|
|
7283
7434
|
*/
|
|
@@ -7301,7 +7452,8 @@ export const tap = core.tap;
|
|
|
7301
7452
|
* If either the success or failure handler fails, the overall effect will also
|
|
7302
7453
|
* fail.
|
|
7303
7454
|
*
|
|
7304
|
-
*
|
|
7455
|
+
* **Example**
|
|
7456
|
+
*
|
|
7305
7457
|
* ```ts
|
|
7306
7458
|
* import { Effect, Random, Console } from "effect"
|
|
7307
7459
|
*
|
|
@@ -7319,7 +7471,7 @@ export const tap = core.tap;
|
|
|
7319
7471
|
* Console.log(`random number: ${randomNumber}`)
|
|
7320
7472
|
* })
|
|
7321
7473
|
*
|
|
7322
|
-
*
|
|
7474
|
+
* Effect.runFork(tapping)
|
|
7323
7475
|
* // Example Output:
|
|
7324
7476
|
* // failure: random number is negative
|
|
7325
7477
|
* ```
|
|
@@ -7344,7 +7496,8 @@ export const tapBoth = effect.tapBoth;
|
|
|
7344
7496
|
* way. Importantly, this does not alter the main result of the effect. If no
|
|
7345
7497
|
* defect occurs, the effect behaves as if this function was not used.
|
|
7346
7498
|
*
|
|
7347
|
-
*
|
|
7499
|
+
* **Example**
|
|
7500
|
+
*
|
|
7348
7501
|
* ```ts
|
|
7349
7502
|
* import { Effect, Console } from "effect"
|
|
7350
7503
|
*
|
|
@@ -7356,7 +7509,7 @@ export const tapBoth = effect.tapBoth;
|
|
|
7356
7509
|
* Console.log(`defect: ${cause}`)
|
|
7357
7510
|
* )
|
|
7358
7511
|
*
|
|
7359
|
-
*
|
|
7512
|
+
* Effect.runFork(tapping1)
|
|
7360
7513
|
* // No Output
|
|
7361
7514
|
*
|
|
7362
7515
|
* // Simulate a severe failure in the system
|
|
@@ -7369,7 +7522,7 @@ export const tapBoth = effect.tapBoth;
|
|
|
7369
7522
|
* Console.log(`defect: ${cause}`)
|
|
7370
7523
|
* )
|
|
7371
7524
|
*
|
|
7372
|
-
*
|
|
7525
|
+
* Effect.runFork(tapping2)
|
|
7373
7526
|
* // Output:
|
|
7374
7527
|
* // defect: RuntimeException: Something went wrong
|
|
7375
7528
|
* // ... stack trace ...
|
|
@@ -7394,7 +7547,8 @@ export const tapDefect = effect.tapDefect;
|
|
|
7394
7547
|
* effect succeeds, the function is ignored, and the success value is propagated
|
|
7395
7548
|
* as usual.
|
|
7396
7549
|
*
|
|
7397
|
-
*
|
|
7550
|
+
* **Example**
|
|
7551
|
+
*
|
|
7398
7552
|
* ```ts
|
|
7399
7553
|
* import { Effect, Console } from "effect"
|
|
7400
7554
|
*
|
|
@@ -7406,7 +7560,7 @@ export const tapDefect = effect.tapDefect;
|
|
|
7406
7560
|
* Console.log(`expected error: ${error}`)
|
|
7407
7561
|
* )
|
|
7408
7562
|
*
|
|
7409
|
-
*
|
|
7563
|
+
* Effect.runFork(tapping)
|
|
7410
7564
|
* // Output:
|
|
7411
7565
|
* // expected error: NetworkError
|
|
7412
7566
|
* ```
|
|
@@ -7429,7 +7583,8 @@ export const tapError = effect.tapError;
|
|
|
7429
7583
|
* If the error doesn't match the specified tag, this function does nothing, and
|
|
7430
7584
|
* the effect proceeds as usual.
|
|
7431
7585
|
*
|
|
7432
|
-
*
|
|
7586
|
+
* **Example**
|
|
7587
|
+
*
|
|
7433
7588
|
* ```ts
|
|
7434
7589
|
* import { Effect, Console } from "effect"
|
|
7435
7590
|
*
|
|
@@ -7452,7 +7607,7 @@ export const tapError = effect.tapError;
|
|
|
7452
7607
|
* Console.log(`expected error: ${error.statusCode}`)
|
|
7453
7608
|
* )
|
|
7454
7609
|
*
|
|
7455
|
-
*
|
|
7610
|
+
* Effect.runFork(tapping)
|
|
7456
7611
|
* // Output:
|
|
7457
7612
|
* // expected error: 504
|
|
7458
7613
|
* ```
|
|
@@ -7476,7 +7631,8 @@ export const tapErrorTag = effect.tapErrorTag;
|
|
|
7476
7631
|
* The effect itself is not modified, and any errors or defects remain in the
|
|
7477
7632
|
* error channel of the original effect.
|
|
7478
7633
|
*
|
|
7479
|
-
*
|
|
7634
|
+
* **Example**
|
|
7635
|
+
*
|
|
7480
7636
|
* ```ts
|
|
7481
7637
|
* import { Effect, Console } from "effect"
|
|
7482
7638
|
*
|
|
@@ -7487,7 +7643,7 @@ export const tapErrorTag = effect.tapErrorTag;
|
|
|
7487
7643
|
* Console.log(`error cause: ${cause}`)
|
|
7488
7644
|
* )
|
|
7489
7645
|
*
|
|
7490
|
-
*
|
|
7646
|
+
* Effect.runFork(tapping1)
|
|
7491
7647
|
* // Output:
|
|
7492
7648
|
* // error cause: Error: NetworkError
|
|
7493
7649
|
*
|
|
@@ -7500,7 +7656,7 @@ export const tapErrorTag = effect.tapErrorTag;
|
|
|
7500
7656
|
* Console.log(`error cause: ${cause}`)
|
|
7501
7657
|
* )
|
|
7502
7658
|
*
|
|
7503
|
-
*
|
|
7659
|
+
* Effect.runFork(tapping2)
|
|
7504
7660
|
* // Output:
|
|
7505
7661
|
* // error cause: RuntimeException: Something went wrong
|
|
7506
7662
|
* // ... stack trace ...
|
|
@@ -7564,9 +7720,9 @@ export const forever = effect.forever;
|
|
|
7564
7720
|
* asynchronous or side-effectful operations, such as polling or iterative
|
|
7565
7721
|
* computations that depend on external factors.
|
|
7566
7722
|
*
|
|
7567
|
-
*
|
|
7723
|
+
* **Example** (Effectful Iteration)
|
|
7724
|
+
*
|
|
7568
7725
|
* ```ts
|
|
7569
|
-
* // Title: Effectful Iteration
|
|
7570
7726
|
* import { Effect } from "effect"
|
|
7571
7727
|
*
|
|
7572
7728
|
* const result = Effect.iterate(
|
|
@@ -7580,7 +7736,7 @@ export const forever = effect.forever;
|
|
|
7580
7736
|
* }
|
|
7581
7737
|
* )
|
|
7582
7738
|
*
|
|
7583
|
-
*
|
|
7739
|
+
* Effect.runPromise(result).then(console.log)
|
|
7584
7740
|
* // Output: 6
|
|
7585
7741
|
* ```
|
|
7586
7742
|
*
|
|
@@ -7629,9 +7785,9 @@ export const iterate = effect.iterate;
|
|
|
7629
7785
|
* computations repeatedly, such as processing items in a list, generating
|
|
7630
7786
|
* values, or performing iterative updates.
|
|
7631
7787
|
*
|
|
7632
|
-
*
|
|
7788
|
+
* **Example** (Looping with Collected Results)
|
|
7789
|
+
*
|
|
7633
7790
|
* ```ts
|
|
7634
|
-
* // Title: Looping with Collected Results
|
|
7635
7791
|
* import { Effect } from "effect"
|
|
7636
7792
|
*
|
|
7637
7793
|
* // A loop that runs 5 times, collecting each iteration's result
|
|
@@ -7648,12 +7804,13 @@ export const iterate = effect.iterate;
|
|
|
7648
7804
|
* }
|
|
7649
7805
|
* )
|
|
7650
7806
|
*
|
|
7651
|
-
*
|
|
7807
|
+
* Effect.runPromise(result).then(console.log)
|
|
7652
7808
|
* // Output: [1, 2, 3, 4, 5]
|
|
7653
7809
|
* ```
|
|
7654
7810
|
*
|
|
7655
|
-
*
|
|
7656
|
-
*
|
|
7811
|
+
* **Example** (Loop with Discarded Results)
|
|
7812
|
+
*
|
|
7813
|
+
* ```ts
|
|
7657
7814
|
* import { Effect, Console } from "effect"
|
|
7658
7815
|
*
|
|
7659
7816
|
* const result = Effect.loop(
|
|
@@ -7671,7 +7828,7 @@ export const iterate = effect.iterate;
|
|
|
7671
7828
|
* }
|
|
7672
7829
|
* )
|
|
7673
7830
|
*
|
|
7674
|
-
*
|
|
7831
|
+
* Effect.runPromise(result).then(console.log)
|
|
7675
7832
|
* // Output:
|
|
7676
7833
|
* // Currently at state 1
|
|
7677
7834
|
* // Currently at state 2
|
|
@@ -7679,6 +7836,7 @@ export const iterate = effect.iterate;
|
|
|
7679
7836
|
* // Currently at state 4
|
|
7680
7837
|
* // Currently at state 5
|
|
7681
7838
|
* // undefined
|
|
7839
|
+
* ```
|
|
7682
7840
|
*
|
|
7683
7841
|
* @since 2.0.0
|
|
7684
7842
|
* @category Looping
|
|
@@ -7706,20 +7864,21 @@ export const loop = effect.loop;
|
|
|
7706
7864
|
* delays, limiting recursions, or dynamically adjusting based on the outcome of
|
|
7707
7865
|
* each execution.
|
|
7708
7866
|
*
|
|
7709
|
-
*
|
|
7867
|
+
* **Example** (Success Example)
|
|
7868
|
+
*
|
|
7710
7869
|
* ```ts
|
|
7711
|
-
* // Success Example
|
|
7712
7870
|
* import { Effect, Schedule, Console } from "effect"
|
|
7713
7871
|
*
|
|
7714
7872
|
* const action = Console.log("success")
|
|
7715
7873
|
* const policy = Schedule.addDelay(Schedule.recurs(2), () => "100 millis")
|
|
7716
7874
|
* const program = Effect.repeat(action, policy)
|
|
7717
7875
|
*
|
|
7718
|
-
*
|
|
7876
|
+
* Effect.runPromise(program).then((n) => console.log(`repetitions: ${n}`))
|
|
7719
7877
|
* ```
|
|
7720
7878
|
*
|
|
7721
|
-
*
|
|
7722
|
-
*
|
|
7879
|
+
* **Example** (Failure Example)
|
|
7880
|
+
*
|
|
7881
|
+
* ```ts
|
|
7723
7882
|
* import { Effect, Schedule } from "effect"
|
|
7724
7883
|
*
|
|
7725
7884
|
* let count = 0
|
|
@@ -7739,7 +7898,8 @@ export const loop = effect.loop;
|
|
|
7739
7898
|
* const policy = Schedule.addDelay(Schedule.recurs(2), () => "100 millis")
|
|
7740
7899
|
* const program = Effect.repeat(action, policy)
|
|
7741
7900
|
*
|
|
7742
|
-
*
|
|
7901
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
7902
|
+
* ```
|
|
7743
7903
|
*
|
|
7744
7904
|
* @since 2.0.0
|
|
7745
7905
|
* @category Repetition / Recursion
|
|
@@ -7763,14 +7923,15 @@ export const repeat = schedule_.repeat_combined;
|
|
|
7763
7923
|
* This function is useful for tasks that need to be retried a fixed number of
|
|
7764
7924
|
* times or for performing repeated actions without requiring a schedule.
|
|
7765
7925
|
*
|
|
7766
|
-
*
|
|
7926
|
+
* **Example**
|
|
7927
|
+
*
|
|
7767
7928
|
* ```ts
|
|
7768
7929
|
* import { Effect, Console } from "effect"
|
|
7769
7930
|
*
|
|
7770
7931
|
* const action = Console.log("success")
|
|
7771
7932
|
* const program = Effect.repeatN(action, 2)
|
|
7772
7933
|
*
|
|
7773
|
-
*
|
|
7934
|
+
* Effect.runPromise(program)
|
|
7774
7935
|
* ```
|
|
7775
7936
|
*
|
|
7776
7937
|
* @since 2.0.0
|
|
@@ -7794,7 +7955,8 @@ export const repeatN = effect.repeatN;
|
|
|
7794
7955
|
* failure occurs during any iteration, the failure handler is invoked to handle
|
|
7795
7956
|
* the situation.
|
|
7796
7957
|
*
|
|
7797
|
-
*
|
|
7958
|
+
* **Example**
|
|
7959
|
+
*
|
|
7798
7960
|
* ```ts
|
|
7799
7961
|
* import { Effect, Schedule } from "effect"
|
|
7800
7962
|
*
|
|
@@ -7824,7 +7986,7 @@ export const repeatN = effect.repeatN;
|
|
|
7824
7986
|
* })
|
|
7825
7987
|
* )
|
|
7826
7988
|
*
|
|
7827
|
-
*
|
|
7989
|
+
* Effect.runPromise(program).then((n) => console.log(`repetitions: ${n}`))
|
|
7828
7990
|
* ```
|
|
7829
7991
|
*
|
|
7830
7992
|
* @since 2.0.0
|
|
@@ -7973,18 +8135,19 @@ export const updateFiberRefs = effect.updateFiberRefs;
|
|
|
7973
8135
|
* The resulting effect cannot fail (`never` in the error channel) but retains
|
|
7974
8136
|
* the context of the original effect.
|
|
7975
8137
|
*
|
|
7976
|
-
*
|
|
8138
|
+
* **Example**
|
|
8139
|
+
*
|
|
7977
8140
|
* ```ts
|
|
7978
8141
|
* import { Effect } from "effect"
|
|
7979
8142
|
*
|
|
7980
8143
|
* const failure = Effect.fail("Uh oh!")
|
|
7981
8144
|
*
|
|
7982
|
-
*
|
|
8145
|
+
* console.log(Effect.runSync(Effect.isFailure(failure)))
|
|
7983
8146
|
* // Output: true
|
|
7984
8147
|
*
|
|
7985
8148
|
* const defect = Effect.dieMessage("BOOM!")
|
|
7986
8149
|
*
|
|
7987
|
-
*
|
|
8150
|
+
* Effect.runSync(Effect.isFailure(defect))
|
|
7988
8151
|
* // throws: BOOM!
|
|
7989
8152
|
* ```
|
|
7990
8153
|
*
|
|
@@ -8024,11 +8187,9 @@ export const isSuccess = effect.isSuccess;
|
|
|
8024
8187
|
* This is useful for structuring your code to respond differently to success or
|
|
8025
8188
|
* failure without triggering side effects.
|
|
8026
8189
|
*
|
|
8027
|
-
*
|
|
8190
|
+
* **Example** (Handling Both Success and Failure Cases)
|
|
8028
8191
|
*
|
|
8029
|
-
* @example
|
|
8030
8192
|
* ```ts
|
|
8031
|
-
* // Title: Handling Both Success and Failure Cases
|
|
8032
8193
|
* import { Effect } from "effect"
|
|
8033
8194
|
*
|
|
8034
8195
|
* const success: Effect.Effect<number, Error> = Effect.succeed(42)
|
|
@@ -8039,7 +8200,7 @@ export const isSuccess = effect.isSuccess;
|
|
|
8039
8200
|
* })
|
|
8040
8201
|
*
|
|
8041
8202
|
* // Run and log the result of the successful effect
|
|
8042
|
-
*
|
|
8203
|
+
* Effect.runPromise(program1).then(console.log)
|
|
8043
8204
|
* // Output: "success: 42"
|
|
8044
8205
|
*
|
|
8045
8206
|
* const failure: Effect.Effect<number, Error> = Effect.fail(
|
|
@@ -8052,10 +8213,12 @@ export const isSuccess = effect.isSuccess;
|
|
|
8052
8213
|
* })
|
|
8053
8214
|
*
|
|
8054
8215
|
* // Run and log the result of the failed effect
|
|
8055
|
-
*
|
|
8216
|
+
* Effect.runPromise(program2).then(console.log)
|
|
8056
8217
|
* // Output: "failure: Uh oh!"
|
|
8057
8218
|
* ```
|
|
8058
8219
|
*
|
|
8220
|
+
* @see {@link matchEffect} if you need to perform side effects in the handlers.
|
|
8221
|
+
*
|
|
8059
8222
|
* @since 2.0.0
|
|
8060
8223
|
* @category Matching
|
|
8061
8224
|
*/
|
|
@@ -8074,13 +8237,9 @@ export const match = effect.match;
|
|
|
8074
8237
|
* regular failures, defects, or interruptions. You can provide specific
|
|
8075
8238
|
* handling logic for each failure type based on the cause.
|
|
8076
8239
|
*
|
|
8077
|
-
*
|
|
8078
|
-
* handlers.
|
|
8079
|
-
* @see {@link match} if you don't need to handle the cause of the failure.
|
|
8240
|
+
* **Example** (Handling Different Failure Causes)
|
|
8080
8241
|
*
|
|
8081
|
-
* @example
|
|
8082
8242
|
* ```ts
|
|
8083
|
-
* // Title: Handling Different Failure Causes
|
|
8084
8243
|
* import { Effect } from "effect"
|
|
8085
8244
|
*
|
|
8086
8245
|
* const task: Effect.Effect<number, Error> = Effect.die("Uh oh!")
|
|
@@ -8106,11 +8265,14 @@ export const match = effect.match;
|
|
|
8106
8265
|
* `succeeded with ${value} value`
|
|
8107
8266
|
* })
|
|
8108
8267
|
*
|
|
8109
|
-
*
|
|
8268
|
+
* Effect.runPromise(program).then(console.log)
|
|
8110
8269
|
* // Output: "Die: Uh oh!"
|
|
8111
|
-
*
|
|
8112
8270
|
* ```
|
|
8113
8271
|
*
|
|
8272
|
+
* @see {@link matchCauseEffect} if you need to perform side effects in the
|
|
8273
|
+
* handlers.
|
|
8274
|
+
* @see {@link match} if you don't need to handle the cause of the failure.
|
|
8275
|
+
*
|
|
8114
8276
|
* @since 2.0.0
|
|
8115
8277
|
* @category Matching
|
|
8116
8278
|
*/
|
|
@@ -8127,12 +8289,9 @@ export const matchCause = core.matchCause;
|
|
|
8127
8289
|
* you to respond accordingly while performing side effects (like logging or
|
|
8128
8290
|
* other operations).
|
|
8129
8291
|
*
|
|
8130
|
-
*
|
|
8131
|
-
* @see {@link matchEffect} if you don't need to handle the cause of the failure.
|
|
8292
|
+
* **Example** (Handling Different Failure Causes with Side Effects)
|
|
8132
8293
|
*
|
|
8133
|
-
* @example
|
|
8134
8294
|
* ```ts
|
|
8135
|
-
* // Title: Handling Different Failure Causes with Side Effects
|
|
8136
8295
|
* import { Effect, Console } from "effect"
|
|
8137
8296
|
*
|
|
8138
8297
|
* const task: Effect.Effect<number, Error> = Effect.die("Uh oh!")
|
|
@@ -8158,10 +8317,13 @@ export const matchCause = core.matchCause;
|
|
|
8158
8317
|
* Console.log(`succeeded with ${value} value`)
|
|
8159
8318
|
* })
|
|
8160
8319
|
*
|
|
8161
|
-
*
|
|
8320
|
+
* Effect.runPromise(program)
|
|
8162
8321
|
* // Output: "Die: Uh oh!"
|
|
8163
8322
|
* ```
|
|
8164
8323
|
*
|
|
8324
|
+
* @see {@link matchCause} if you don't need side effects and only want to handle the result or failure.
|
|
8325
|
+
* @see {@link matchEffect} if you don't need to handle the cause of the failure.
|
|
8326
|
+
*
|
|
8165
8327
|
* @since 2.0.0
|
|
8166
8328
|
* @category Matching
|
|
8167
8329
|
*/
|
|
@@ -8180,12 +8342,9 @@ export const matchCauseEffect = core.matchCauseEffect;
|
|
|
8180
8342
|
* This is useful when you need to execute additional actions, like logging or
|
|
8181
8343
|
* notifying users, based on whether an effect succeeds or fails.
|
|
8182
8344
|
*
|
|
8183
|
-
*
|
|
8184
|
-
* result or failure.
|
|
8345
|
+
* **Example** (Handling Both Success and Failure Cases with Side Effects)
|
|
8185
8346
|
*
|
|
8186
|
-
* @example
|
|
8187
8347
|
* ```ts
|
|
8188
|
-
* // Title: Handling Both Success and Failure Cases with Side Effects
|
|
8189
8348
|
* import { Effect } from "effect"
|
|
8190
8349
|
*
|
|
8191
8350
|
* const success: Effect.Effect<number, Error> = Effect.succeed(42)
|
|
@@ -8222,6 +8381,9 @@ export const matchCauseEffect = core.matchCauseEffect;
|
|
|
8222
8381
|
* // failure: Uh oh!
|
|
8223
8382
|
* ```
|
|
8224
8383
|
*
|
|
8384
|
+
* @see {@link match} if you don't need side effects and only want to handle the
|
|
8385
|
+
* result or failure.
|
|
8386
|
+
*
|
|
8225
8387
|
* @since 2.0.0
|
|
8226
8388
|
* @category Matching
|
|
8227
8389
|
*/
|
|
@@ -8242,7 +8404,8 @@ export const matchEffect = core.matchEffect;
|
|
|
8242
8404
|
* level, and fiber ID, making it suitable for debugging and tracking purposes.
|
|
8243
8405
|
* This function does not interrupt or alter the effect's execution flow.
|
|
8244
8406
|
*
|
|
8245
|
-
*
|
|
8407
|
+
* **Example**
|
|
8408
|
+
*
|
|
8246
8409
|
* ```ts
|
|
8247
8410
|
* import { Cause, Effect } from "effect"
|
|
8248
8411
|
*
|
|
@@ -8253,7 +8416,7 @@ export const matchEffect = core.matchEffect;
|
|
|
8253
8416
|
* Cause.die("Oh uh!")
|
|
8254
8417
|
* )
|
|
8255
8418
|
*
|
|
8256
|
-
*
|
|
8419
|
+
* Effect.runFork(program)
|
|
8257
8420
|
* // Output:
|
|
8258
8421
|
* // timestamp=... level=INFO fiber=#0 message=message1 message=message2 cause="Error: Oh no!
|
|
8259
8422
|
* // Error: Oh uh!"
|
|
@@ -8273,7 +8436,8 @@ export const log = effect.log;
|
|
|
8273
8436
|
* flexibility in categorizing logs based on their importance or severity,
|
|
8274
8437
|
* making it easier to filter logs during debugging or production monitoring.
|
|
8275
8438
|
*
|
|
8276
|
-
*
|
|
8439
|
+
* **Example**
|
|
8440
|
+
*
|
|
8277
8441
|
* ```ts
|
|
8278
8442
|
* import { Cause, Effect, LogLevel } from "effect"
|
|
8279
8443
|
*
|
|
@@ -8283,7 +8447,7 @@ export const log = effect.log;
|
|
|
8283
8447
|
* Cause.die("System failure!")
|
|
8284
8448
|
* )
|
|
8285
8449
|
*
|
|
8286
|
-
*
|
|
8450
|
+
* Effect.runFork(program)
|
|
8287
8451
|
* // Output:
|
|
8288
8452
|
* // timestamp=... level=ERROR fiber=#0 message=Critical error encountered cause="Error: System failure!"
|
|
8289
8453
|
* ```
|
|
@@ -8303,13 +8467,14 @@ export const logWithLevel = (level, ...message) => effect.logWithLevel(level)(..
|
|
|
8303
8467
|
* configuration by setting the minimum log level to `LogLevel.Trace` using
|
|
8304
8468
|
* `Logger.withMinimumLogLevel`.
|
|
8305
8469
|
*
|
|
8306
|
-
*
|
|
8470
|
+
* **Example**
|
|
8471
|
+
*
|
|
8307
8472
|
* ```ts
|
|
8308
8473
|
* import { Effect, Logger, LogLevel } from "effect"
|
|
8309
8474
|
*
|
|
8310
8475
|
* const program = Effect.logTrace("message1").pipe(Logger.withMinimumLogLevel(LogLevel.Trace))
|
|
8311
8476
|
*
|
|
8312
|
-
*
|
|
8477
|
+
* Effect.runFork(program)
|
|
8313
8478
|
* // timestamp=... level=TRACE fiber=#0 message=message1
|
|
8314
8479
|
* ```
|
|
8315
8480
|
*
|
|
@@ -8327,13 +8492,14 @@ export const logTrace = effect.logTrace;
|
|
|
8327
8492
|
* less detailed information than TRACE logs but are still not shown by default.
|
|
8328
8493
|
* To view these logs, adjust the log level using `Logger.withMinimumLogLevel`.
|
|
8329
8494
|
*
|
|
8330
|
-
*
|
|
8495
|
+
* **Example**
|
|
8496
|
+
*
|
|
8331
8497
|
* ```ts
|
|
8332
8498
|
* import { Effect, Logger, LogLevel } from "effect"
|
|
8333
8499
|
*
|
|
8334
8500
|
* const program = Effect.logDebug("message1").pipe(Logger.withMinimumLogLevel(LogLevel.Debug))
|
|
8335
8501
|
*
|
|
8336
|
-
*
|
|
8502
|
+
* Effect.runFork(program)
|
|
8337
8503
|
* // timestamp=... level=DEBUG fiber=#0 message=message1
|
|
8338
8504
|
* ```
|
|
8339
8505
|
*
|
|
@@ -8410,7 +8576,8 @@ export const logFatal = effect.logFatal;
|
|
|
8410
8576
|
* operation. The span information is included in the log metadata, making it
|
|
8411
8577
|
* easy to trace performance metrics in logs.
|
|
8412
8578
|
*
|
|
8413
|
-
*
|
|
8579
|
+
* **Example**
|
|
8580
|
+
*
|
|
8414
8581
|
* ```ts
|
|
8415
8582
|
* import { Effect } from "effect"
|
|
8416
8583
|
*
|
|
@@ -8419,7 +8586,7 @@ export const logFatal = effect.logFatal;
|
|
|
8419
8586
|
* yield* Effect.log("The job is finished!")
|
|
8420
8587
|
* }).pipe(Effect.withLogSpan("myspan"))
|
|
8421
8588
|
*
|
|
8422
|
-
*
|
|
8589
|
+
* Effect.runFork(program)
|
|
8423
8590
|
* // timestamp=... level=INFO fiber=#0 message="The job is finished!" myspan=1011ms
|
|
8424
8591
|
* ```
|
|
8425
8592
|
*
|
|
@@ -8445,9 +8612,8 @@ export const withLogSpan = effect.withLogSpan;
|
|
|
8445
8612
|
* The annotated key-value pairs will appear alongside the log message in the
|
|
8446
8613
|
* output.
|
|
8447
8614
|
*
|
|
8448
|
-
*
|
|
8615
|
+
* **Example**
|
|
8449
8616
|
*
|
|
8450
|
-
* @example
|
|
8451
8617
|
* ```ts
|
|
8452
8618
|
* import { Effect } from "effect"
|
|
8453
8619
|
*
|
|
@@ -8456,11 +8622,13 @@ export const withLogSpan = effect.withLogSpan;
|
|
|
8456
8622
|
* yield* Effect.log("message2")
|
|
8457
8623
|
* }).pipe(Effect.annotateLogs("taskId", "1234")) // Annotation as key/value pair
|
|
8458
8624
|
*
|
|
8459
|
-
*
|
|
8625
|
+
* Effect.runFork(program)
|
|
8460
8626
|
* // timestamp=... level=INFO fiber=#0 message=message1 taskId=1234
|
|
8461
8627
|
* // timestamp=... level=INFO fiber=#0 message=message2 taskId=1234
|
|
8462
8628
|
* ```
|
|
8463
8629
|
*
|
|
8630
|
+
* @see {@link annotateLogsScoped} to add log annotations with a limited scope.
|
|
8631
|
+
*
|
|
8464
8632
|
* @since 2.0.0
|
|
8465
8633
|
* @category Logging
|
|
8466
8634
|
*/
|
|
@@ -8481,9 +8649,8 @@ export const annotateLogs = effect.annotateLogs;
|
|
|
8481
8649
|
* multiple key-value pairs. This flexibility enables fine-grained control over
|
|
8482
8650
|
* the additional metadata included in logs for specific tasks or operations.
|
|
8483
8651
|
*
|
|
8484
|
-
*
|
|
8652
|
+
* **Example**
|
|
8485
8653
|
*
|
|
8486
|
-
* @example
|
|
8487
8654
|
* ```ts
|
|
8488
8655
|
* import { Effect } from "effect"
|
|
8489
8656
|
*
|
|
@@ -8494,13 +8661,15 @@ export const annotateLogs = effect.annotateLogs;
|
|
|
8494
8661
|
* yield* Effect.log("message2") // Annotation is applied to this log
|
|
8495
8662
|
* }).pipe(Effect.scoped, Effect.andThen(Effect.log("no annotations again")))
|
|
8496
8663
|
*
|
|
8497
|
-
*
|
|
8664
|
+
* Effect.runFork(program)
|
|
8498
8665
|
* // timestamp=... level=INFO fiber=#0 message="no annotations"
|
|
8499
8666
|
* // timestamp=... level=INFO fiber=#0 message=message1 key=value
|
|
8500
8667
|
* // timestamp=... level=INFO fiber=#0 message=message2 key=value
|
|
8501
8668
|
* // timestamp=... level=INFO fiber=#0 message="no annotations again"
|
|
8502
8669
|
* ```
|
|
8503
8670
|
*
|
|
8671
|
+
* @see {@link annotateLogs} to add custom annotations to log entries generated within an effect.
|
|
8672
|
+
*
|
|
8504
8673
|
* @since 3.1.0
|
|
8505
8674
|
* @category Logging
|
|
8506
8675
|
*/
|
|
@@ -8543,7 +8712,8 @@ export const logAnnotations = effect.logAnnotations;
|
|
|
8543
8712
|
* It is especially useful when you want to reduce noise in logs or prioritize
|
|
8544
8713
|
* certain types of errors.
|
|
8545
8714
|
*
|
|
8546
|
-
*
|
|
8715
|
+
* **Example**
|
|
8716
|
+
*
|
|
8547
8717
|
* ```ts
|
|
8548
8718
|
* import { Effect, Fiber, LogLevel, Option } from "effect"
|
|
8549
8719
|
*
|
|
@@ -8552,7 +8722,7 @@ export const logAnnotations = effect.logAnnotations;
|
|
|
8552
8722
|
* yield* Fiber.join(fiber)
|
|
8553
8723
|
* })
|
|
8554
8724
|
*
|
|
8555
|
-
*
|
|
8725
|
+
* Effect.runFork(program.pipe(Effect.withUnhandledErrorLogLevel(Option.some(LogLevel.Error))))
|
|
8556
8726
|
* // Output:
|
|
8557
8727
|
* // timestamp=... level=ERROR fiber=#1 message="Fiber terminated with an unhandled error" cause="Error: Unhandled error!"
|
|
8558
8728
|
* ```
|
|
@@ -8574,7 +8744,8 @@ export const withUnhandledErrorLogLevel = core.withUnhandledErrorLogLevel;
|
|
|
8574
8744
|
* This function is useful for conditionally executing logging-related effects
|
|
8575
8745
|
* or other operations that depend on the current log level configuration.
|
|
8576
8746
|
*
|
|
8577
|
-
*
|
|
8747
|
+
* **Example**
|
|
8748
|
+
*
|
|
8578
8749
|
* ```ts
|
|
8579
8750
|
* import { Effect, Logger, LogLevel } from "effect"
|
|
8580
8751
|
*
|
|
@@ -8583,7 +8754,7 @@ export const withUnhandledErrorLogLevel = core.withUnhandledErrorLogLevel;
|
|
|
8583
8754
|
* yield* Effect.whenLogLevel(Effect.logDebug("message2"), LogLevel.Debug); // returns `Some`
|
|
8584
8755
|
* }).pipe(Logger.withMinimumLogLevel(LogLevel.Debug));
|
|
8585
8756
|
*
|
|
8586
|
-
*
|
|
8757
|
+
* Effect.runFork(program)
|
|
8587
8758
|
* // timestamp=... level=DEBUG fiber=#0 message=message2
|
|
8588
8759
|
* ```
|
|
8589
8760
|
*
|
|
@@ -8610,11 +8781,9 @@ export const whenLogLevel = fiberRuntime.whenLogLevel;
|
|
|
8610
8781
|
* Use `orDie` when failures should be treated as unrecoverable defects and no
|
|
8611
8782
|
* error handling is required.
|
|
8612
8783
|
*
|
|
8613
|
-
*
|
|
8784
|
+
* **Example** (Propagating an Error as a Defect)
|
|
8614
8785
|
*
|
|
8615
|
-
* @example
|
|
8616
8786
|
* ```ts
|
|
8617
|
-
* // Title: Propagating an Error as a Defect
|
|
8618
8787
|
* import { Effect } from "effect"
|
|
8619
8788
|
*
|
|
8620
8789
|
* const divide = (a: number, b: number) =>
|
|
@@ -8626,12 +8795,14 @@ export const whenLogLevel = fiberRuntime.whenLogLevel;
|
|
|
8626
8795
|
* // ▼
|
|
8627
8796
|
* const program = Effect.orDie(divide(1, 0))
|
|
8628
8797
|
*
|
|
8629
|
-
*
|
|
8798
|
+
* Effect.runPromise(program).catch(console.error)
|
|
8630
8799
|
* // Output:
|
|
8631
8800
|
* // (FiberFailure) Error: Cannot divide by zero
|
|
8632
8801
|
* // ...stack trace...
|
|
8633
8802
|
* ```
|
|
8634
8803
|
*
|
|
8804
|
+
* @see {@link orDieWith} if you need to customize the error.
|
|
8805
|
+
*
|
|
8635
8806
|
* @since 2.0.0
|
|
8636
8807
|
* @category Converting Failures to Defects
|
|
8637
8808
|
*/
|
|
@@ -8651,11 +8822,9 @@ export const orDie = core.orDie;
|
|
|
8651
8822
|
* Use `orDieWith` when failures should terminate the fiber as defects, and you want to customize
|
|
8652
8823
|
* the error for clarity or debugging purposes.
|
|
8653
8824
|
*
|
|
8654
|
-
*
|
|
8825
|
+
* **Example** (Customizing Defect)
|
|
8655
8826
|
*
|
|
8656
|
-
* @example
|
|
8657
8827
|
* ```ts
|
|
8658
|
-
* // Title: Customizing Defect
|
|
8659
8828
|
* import { Effect } from "effect"
|
|
8660
8829
|
*
|
|
8661
8830
|
* const divide = (a: number, b: number) =>
|
|
@@ -8670,12 +8839,14 @@ export const orDie = core.orDie;
|
|
|
8670
8839
|
* (error) => new Error(`defect: ${error.message}`)
|
|
8671
8840
|
* )
|
|
8672
8841
|
*
|
|
8673
|
-
*
|
|
8842
|
+
* Effect.runPromise(program).catch(console.error)
|
|
8674
8843
|
* // Output:
|
|
8675
8844
|
* // (FiberFailure) Error: defect: Cannot divide by zero
|
|
8676
8845
|
* // ...stack trace...
|
|
8677
8846
|
* ```
|
|
8678
8847
|
*
|
|
8848
|
+
* @see {@link orDie} if you don't need to customize the error.
|
|
8849
|
+
*
|
|
8679
8850
|
* @since 2.0.0
|
|
8680
8851
|
* @category Converting Failures to Defects
|
|
8681
8852
|
*/
|
|
@@ -8694,9 +8865,8 @@ export const orDieWith = core.orDieWith;
|
|
|
8694
8865
|
* The error type of the resulting effect will be that of the fallback effect,
|
|
8695
8866
|
* as the first effect's error is replaced when the fallback is executed.
|
|
8696
8867
|
*
|
|
8697
|
-
*
|
|
8868
|
+
* **Example**
|
|
8698
8869
|
*
|
|
8699
|
-
* @example
|
|
8700
8870
|
* ```ts
|
|
8701
8871
|
* import { Effect } from "effect"
|
|
8702
8872
|
*
|
|
@@ -8715,6 +8885,8 @@ export const orDieWith = core.orDieWith;
|
|
|
8715
8885
|
* // Output: "fallback"
|
|
8716
8886
|
* ```
|
|
8717
8887
|
*
|
|
8888
|
+
* @see {@link catchAll} if you need to access the error in the fallback effect.
|
|
8889
|
+
*
|
|
8718
8890
|
* @since 2.0.0
|
|
8719
8891
|
* @category Fallback
|
|
8720
8892
|
*/
|
|
@@ -8737,9 +8909,8 @@ export const orElse = core.orElse;
|
|
|
8737
8909
|
* error management by ensuring that all failures are replaced with a controlled
|
|
8738
8910
|
* alternative.
|
|
8739
8911
|
*
|
|
8740
|
-
*
|
|
8912
|
+
* **Example**
|
|
8741
8913
|
*
|
|
8742
|
-
* @example
|
|
8743
8914
|
* ```ts
|
|
8744
8915
|
* import { Effect } from "effect"
|
|
8745
8916
|
*
|
|
@@ -8764,6 +8935,8 @@ export const orElse = core.orElse;
|
|
|
8764
8935
|
* // }
|
|
8765
8936
|
* ```
|
|
8766
8937
|
*
|
|
8938
|
+
* @see {@link mapError} if you need to access the error to transform it.
|
|
8939
|
+
*
|
|
8767
8940
|
* @since 2.0.0
|
|
8768
8941
|
* @category Fallback
|
|
8769
8942
|
*/
|
|
@@ -8787,7 +8960,8 @@ export const orElseFail = effect.orElseFail;
|
|
|
8787
8960
|
* function, you can avoid the need for complex error handling and guarantee a
|
|
8788
8961
|
* fallback result.
|
|
8789
8962
|
*
|
|
8790
|
-
*
|
|
8963
|
+
* **Example**
|
|
8964
|
+
*
|
|
8791
8965
|
* ```ts
|
|
8792
8966
|
* import { Effect } from "effect"
|
|
8793
8967
|
*
|
|
@@ -8837,7 +9011,8 @@ export const orElseSucceed = effect.orElseSucceed;
|
|
|
8837
9011
|
* alternative sources to obtain a result, such as attempting multiple APIs,
|
|
8838
9012
|
* retrieving configurations, or accessing resources in a prioritized manner.
|
|
8839
9013
|
*
|
|
8840
|
-
*
|
|
9014
|
+
* **Example**
|
|
9015
|
+
*
|
|
8841
9016
|
* ```ts
|
|
8842
9017
|
* import { Effect, Console } from "effect"
|
|
8843
9018
|
*
|
|
@@ -9047,7 +9222,8 @@ export const unsafeMakeSemaphore = circular.unsafeMakeSemaphore;
|
|
|
9047
9222
|
* shared resource. The number of permits determines how many tasks can access
|
|
9048
9223
|
* the resource concurrently.
|
|
9049
9224
|
*
|
|
9050
|
-
*
|
|
9225
|
+
* **Example**
|
|
9226
|
+
*
|
|
9051
9227
|
* ```ts
|
|
9052
9228
|
* import { Effect } from "effect"
|
|
9053
9229
|
*
|
|
@@ -9072,7 +9248,8 @@ export const unsafeMakeLatch = circular.unsafeMakeLatch;
|
|
|
9072
9248
|
* This function initializes a `Latch` safely, ensuring proper runtime
|
|
9073
9249
|
* guarantees. By default, the latch starts in the closed state.
|
|
9074
9250
|
*
|
|
9075
|
-
*
|
|
9251
|
+
* **Example**
|
|
9252
|
+
*
|
|
9076
9253
|
* ```ts
|
|
9077
9254
|
* import { Console, Effect } from "effect"
|
|
9078
9255
|
*
|
|
@@ -9093,7 +9270,7 @@ export const unsafeMakeLatch = circular.unsafeMakeLatch;
|
|
|
9093
9270
|
* yield* fiber.await
|
|
9094
9271
|
* })
|
|
9095
9272
|
*
|
|
9096
|
-
*
|
|
9273
|
+
* Effect.runFork(program)
|
|
9097
9274
|
* // Output: open sesame (after 1 second)
|
|
9098
9275
|
* ```
|
|
9099
9276
|
*
|
|
@@ -9131,9 +9308,9 @@ export const makeLatch = circular.makeLatch;
|
|
|
9131
9308
|
* This function is ideal if you don't need the result immediately or if the
|
|
9132
9309
|
* effect is part of a larger concurrent workflow.
|
|
9133
9310
|
*
|
|
9134
|
-
*
|
|
9311
|
+
* **Example** (Running an Effect in the Background)
|
|
9312
|
+
*
|
|
9135
9313
|
* ```ts
|
|
9136
|
-
* // Title: Running an Effect in the Background
|
|
9137
9314
|
* import { Effect, Console, Schedule, Fiber } from "effect"
|
|
9138
9315
|
*
|
|
9139
9316
|
* // ┌─── Effect<number, never, never>
|
|
@@ -9194,25 +9371,27 @@ export const runCallback = runtime_.unsafeRunEffect;
|
|
|
9194
9371
|
* in a promise-based system, such as when integrating with third-party
|
|
9195
9372
|
* libraries that expect `Promise` results.
|
|
9196
9373
|
*
|
|
9197
|
-
*
|
|
9198
|
-
* of rejecting.
|
|
9374
|
+
* **Example** (Running a Successful Effect as a Promise)
|
|
9199
9375
|
*
|
|
9200
|
-
* @example
|
|
9201
9376
|
* ```ts
|
|
9202
|
-
* // Title: Running a Successful Effect as a Promise
|
|
9203
9377
|
* import { Effect } from "effect"
|
|
9204
9378
|
*
|
|
9205
|
-
*
|
|
9379
|
+
* Effect.runPromise(Effect.succeed(1)).then(console.log)
|
|
9206
9380
|
* // Output: 1
|
|
9207
9381
|
* ```
|
|
9208
9382
|
*
|
|
9209
|
-
*
|
|
9210
|
-
*
|
|
9383
|
+
* **Example** (Handling a Failing Effect as a Rejected Promise)
|
|
9384
|
+
*
|
|
9385
|
+
* ```ts
|
|
9211
9386
|
* import { Effect } from "effect"
|
|
9212
9387
|
*
|
|
9213
|
-
*
|
|
9388
|
+
* Effect.runPromise(Effect.fail("my error")).catch(console.error)
|
|
9214
9389
|
* // Output:
|
|
9215
9390
|
* // (FiberFailure) Error: my error
|
|
9391
|
+
* ```
|
|
9392
|
+
*
|
|
9393
|
+
* @see {@link runPromiseExit} for a version that returns an `Exit` type instead
|
|
9394
|
+
* of rejecting.
|
|
9216
9395
|
*
|
|
9217
9396
|
* @since 2.0.0
|
|
9218
9397
|
* @category Running Effects
|
|
@@ -9243,13 +9422,13 @@ export const runPromise = runtime_.unsafeRunPromiseEffect;
|
|
|
9243
9422
|
* that rely on promises but need more detailed error handling than a simple
|
|
9244
9423
|
* rejection.
|
|
9245
9424
|
*
|
|
9246
|
-
*
|
|
9425
|
+
* **Example** (Handling Results as Exit)
|
|
9426
|
+
*
|
|
9247
9427
|
* ```ts
|
|
9248
|
-
* // Title: Handling Results as Exit
|
|
9249
9428
|
* import { Effect } from "effect"
|
|
9250
9429
|
*
|
|
9251
9430
|
* // Execute a successful effect and get the Exit result as a Promise
|
|
9252
|
-
*
|
|
9431
|
+
* Effect.runPromiseExit(Effect.succeed(1)).then(console.log)
|
|
9253
9432
|
* // Output:
|
|
9254
9433
|
* // {
|
|
9255
9434
|
* // _id: "Exit",
|
|
@@ -9258,7 +9437,7 @@ export const runPromise = runtime_.unsafeRunPromiseEffect;
|
|
|
9258
9437
|
* // }
|
|
9259
9438
|
*
|
|
9260
9439
|
* // Execute a failing effect and get the Exit result as a Promise
|
|
9261
|
-
*
|
|
9440
|
+
* Effect.runPromiseExit(Effect.fail("my error")).then(console.log)
|
|
9262
9441
|
* // Output:
|
|
9263
9442
|
* // {
|
|
9264
9443
|
* // _id: "Exit",
|
|
@@ -9305,12 +9484,9 @@ export const runPromiseExit = runtime_.unsafeRunPromiseExitEffect;
|
|
|
9305
9484
|
* handling. For such cases, consider using {@link runPromise} or
|
|
9306
9485
|
* {@link runSyncExit}.
|
|
9307
9486
|
*
|
|
9308
|
-
*
|
|
9309
|
-
* throwing an error.
|
|
9487
|
+
* **Example** (Synchronous Logging)
|
|
9310
9488
|
*
|
|
9311
|
-
* @example
|
|
9312
9489
|
* ```ts
|
|
9313
|
-
* // Title: Synchronous Logging
|
|
9314
9490
|
* import { Effect } from "effect"
|
|
9315
9491
|
*
|
|
9316
9492
|
* const program = Effect.sync(() => {
|
|
@@ -9325,8 +9501,9 @@ export const runPromiseExit = runtime_.unsafeRunPromiseExitEffect;
|
|
|
9325
9501
|
* // Output: 1
|
|
9326
9502
|
* ```
|
|
9327
9503
|
*
|
|
9328
|
-
*
|
|
9329
|
-
*
|
|
9504
|
+
* **Example** (Incorrect Usage with Failing or Async Effects)
|
|
9505
|
+
*
|
|
9506
|
+
* ```ts
|
|
9330
9507
|
* import { Effect } from "effect"
|
|
9331
9508
|
*
|
|
9332
9509
|
* try {
|
|
@@ -9346,6 +9523,10 @@ export const runPromiseExit = runtime_.unsafeRunPromiseExitEffect;
|
|
|
9346
9523
|
* }
|
|
9347
9524
|
* // Output:
|
|
9348
9525
|
* // (FiberFailure) AsyncFiberException: Fiber #0 cannot be resolved synchronously. This is caused by using runSync on an effect that performs async work
|
|
9526
|
+
* ```
|
|
9527
|
+
*
|
|
9528
|
+
* @see {@link runSyncExit} for a version that returns an `Exit` type instead of
|
|
9529
|
+
* throwing an error.
|
|
9349
9530
|
*
|
|
9350
9531
|
* @since 2.0.0
|
|
9351
9532
|
* @category Running Effects
|
|
@@ -9376,9 +9557,9 @@ export const runSync = runtime_.unsafeRunSyncEffect;
|
|
|
9376
9557
|
*
|
|
9377
9558
|
* Avoid using this function for effects that involve asynchronous operations, as it will fail with a `Die` cause.
|
|
9378
9559
|
*
|
|
9379
|
-
*
|
|
9560
|
+
* **Example** (Handling Results as Exit)
|
|
9561
|
+
*
|
|
9380
9562
|
* ```ts
|
|
9381
|
-
* // Title: Handling Results as Exit
|
|
9382
9563
|
* import { Effect } from "effect"
|
|
9383
9564
|
*
|
|
9384
9565
|
* console.log(Effect.runSyncExit(Effect.succeed(1)))
|
|
@@ -9402,8 +9583,9 @@ export const runSync = runtime_.unsafeRunSyncEffect;
|
|
|
9402
9583
|
* // }
|
|
9403
9584
|
* ```
|
|
9404
9585
|
*
|
|
9405
|
-
*
|
|
9406
|
-
*
|
|
9586
|
+
* **Example** (Asynchronous Operation Resulting in Die)
|
|
9587
|
+
*
|
|
9588
|
+
* ```ts
|
|
9407
9589
|
* import { Effect } from "effect"
|
|
9408
9590
|
*
|
|
9409
9591
|
* console.log(Effect.runSyncExit(Effect.promise(() => Promise.resolve(1))))
|
|
@@ -9421,6 +9603,7 @@ export const runSync = runtime_.unsafeRunSyncEffect;
|
|
|
9421
9603
|
* // }
|
|
9422
9604
|
* // }
|
|
9423
9605
|
* // }
|
|
9606
|
+
* ```
|
|
9424
9607
|
*
|
|
9425
9608
|
* @since 2.0.0
|
|
9426
9609
|
* @category Running Effects
|
|
@@ -9441,9 +9624,8 @@ export const runSyncExit = runtime_.unsafeRunSyncExitEffect;
|
|
|
9441
9624
|
* provides flexibility in scenarios where you want to maximize performance or
|
|
9442
9625
|
* ensure specific ordering.
|
|
9443
9626
|
*
|
|
9444
|
-
*
|
|
9627
|
+
* **Example**
|
|
9445
9628
|
*
|
|
9446
|
-
* @example
|
|
9447
9629
|
* ```ts
|
|
9448
9630
|
* import { Effect, Console } from "effect"
|
|
9449
9631
|
*
|
|
@@ -9458,7 +9640,7 @@ export const runSyncExit = runtime_.unsafeRunSyncExitEffect;
|
|
|
9458
9640
|
* Effect.validate(task4)
|
|
9459
9641
|
* )
|
|
9460
9642
|
*
|
|
9461
|
-
*
|
|
9643
|
+
* Effect.runPromiseExit(program).then(console.log)
|
|
9462
9644
|
* // Output:
|
|
9463
9645
|
* // task1
|
|
9464
9646
|
* // task2
|
|
@@ -9474,6 +9656,8 @@ export const runSyncExit = runtime_.unsafeRunSyncExitEffect;
|
|
|
9474
9656
|
* // }
|
|
9475
9657
|
* ```
|
|
9476
9658
|
*
|
|
9659
|
+
* @see {@link zip} for a version that stops at the first error.
|
|
9660
|
+
*
|
|
9477
9661
|
* @since 2.0.0
|
|
9478
9662
|
* @category Error Accumulation
|
|
9479
9663
|
*/
|
|
@@ -9513,13 +9697,9 @@ export const validateWith = fiberRuntime.validateWith;
|
|
|
9513
9697
|
* is set to `true`, the effects will run concurrently, potentially improving
|
|
9514
9698
|
* performance for independent operations.
|
|
9515
9699
|
*
|
|
9516
|
-
*
|
|
9517
|
-
* function.
|
|
9518
|
-
* @see {@link validate} for a version that accumulates errors.
|
|
9700
|
+
* **Example** (Combining Two Effects Sequentially)
|
|
9519
9701
|
*
|
|
9520
|
-
* @example
|
|
9521
9702
|
* ```ts
|
|
9522
|
-
* // Title: Combining Two Effects Sequentially
|
|
9523
9703
|
* import { Effect } from "effect"
|
|
9524
9704
|
*
|
|
9525
9705
|
* const task1 = Effect.succeed(1).pipe(
|
|
@@ -9537,15 +9717,16 @@ export const validateWith = fiberRuntime.validateWith;
|
|
|
9537
9717
|
* // ▼
|
|
9538
9718
|
* const program = Effect.zip(task1, task2)
|
|
9539
9719
|
*
|
|
9540
|
-
*
|
|
9720
|
+
* Effect.runPromise(program).then(console.log)
|
|
9541
9721
|
* // Output:
|
|
9542
9722
|
* // timestamp=... level=INFO fiber=#0 message="task1 done"
|
|
9543
9723
|
* // timestamp=... level=INFO fiber=#0 message="task2 done"
|
|
9544
9724
|
* // [ 1, 'hello' ]
|
|
9545
9725
|
* ```
|
|
9546
9726
|
*
|
|
9547
|
-
*
|
|
9548
|
-
*
|
|
9727
|
+
* **Example** (Combining Two Effects Concurrently)
|
|
9728
|
+
*
|
|
9729
|
+
* ```ts
|
|
9549
9730
|
* import { Effect } from "effect"
|
|
9550
9731
|
*
|
|
9551
9732
|
* const task1 = Effect.succeed(1).pipe(
|
|
@@ -9560,11 +9741,16 @@ export const validateWith = fiberRuntime.validateWith;
|
|
|
9560
9741
|
* // Run both effects concurrently using the concurrent option
|
|
9561
9742
|
* const program = Effect.zip(task1, task2, { concurrent: true })
|
|
9562
9743
|
*
|
|
9563
|
-
*
|
|
9744
|
+
* Effect.runPromise(program).then(console.log)
|
|
9564
9745
|
* // Output:
|
|
9565
9746
|
* // timestamp=... level=INFO fiber=#0 message="task2 done"
|
|
9566
9747
|
* // timestamp=... level=INFO fiber=#0 message="task1 done"
|
|
9567
9748
|
* // [ 1, 'hello' ]
|
|
9749
|
+
* ```
|
|
9750
|
+
*
|
|
9751
|
+
* @see {@link zipWith} for a version that combines the results with a custom
|
|
9752
|
+
* function.
|
|
9753
|
+
* @see {@link validate} for a version that accumulates errors.
|
|
9568
9754
|
*
|
|
9569
9755
|
* @since 2.0.0
|
|
9570
9756
|
* @category Zipping
|
|
@@ -9591,10 +9777,8 @@ export const zip = fiberRuntime.zipOptions;
|
|
|
9591
9777
|
* effect but still need to run the second effect for its side effects, such as
|
|
9592
9778
|
* logging or performing a cleanup action.
|
|
9593
9779
|
*
|
|
9594
|
-
*
|
|
9595
|
-
* effect.
|
|
9780
|
+
* **Example**
|
|
9596
9781
|
*
|
|
9597
|
-
* @example
|
|
9598
9782
|
* ```ts
|
|
9599
9783
|
* import { Effect } from "effect"
|
|
9600
9784
|
*
|
|
@@ -9609,13 +9793,16 @@ export const zip = fiberRuntime.zipOptions;
|
|
|
9609
9793
|
*
|
|
9610
9794
|
* const program = Effect.zipLeft(task1, task2)
|
|
9611
9795
|
*
|
|
9612
|
-
*
|
|
9796
|
+
* Effect.runPromise(program).then(console.log)
|
|
9613
9797
|
* // Output:
|
|
9614
9798
|
* // timestamp=... level=INFO fiber=#0 message="task1 done"
|
|
9615
9799
|
* // timestamp=... level=INFO fiber=#0 message="task2 done"
|
|
9616
9800
|
* // 1
|
|
9617
9801
|
* ```
|
|
9618
9802
|
*
|
|
9803
|
+
* @see {@link zipRight} for a version that returns the result of the second
|
|
9804
|
+
* effect.
|
|
9805
|
+
*
|
|
9619
9806
|
* @since 2.0.0
|
|
9620
9807
|
* @category Zipping
|
|
9621
9808
|
*/
|
|
@@ -9641,10 +9828,8 @@ export const zipLeft = fiberRuntime.zipLeftOptions;
|
|
|
9641
9828
|
* effect but still need to run the first effect for its side effects, such as
|
|
9642
9829
|
* initialization or setup tasks.
|
|
9643
9830
|
*
|
|
9644
|
-
*
|
|
9645
|
-
* effect.
|
|
9831
|
+
* **Example**
|
|
9646
9832
|
*
|
|
9647
|
-
* @example
|
|
9648
9833
|
* ```ts
|
|
9649
9834
|
* import { Effect } from "effect"
|
|
9650
9835
|
*
|
|
@@ -9659,13 +9844,16 @@ export const zipLeft = fiberRuntime.zipLeftOptions;
|
|
|
9659
9844
|
*
|
|
9660
9845
|
* const program = Effect.zipRight(task1, task2)
|
|
9661
9846
|
*
|
|
9662
|
-
*
|
|
9847
|
+
* Effect.runPromise(program).then(console.log)
|
|
9663
9848
|
* // Output:
|
|
9664
9849
|
* // timestamp=... level=INFO fiber=#0 message="task1 done"
|
|
9665
9850
|
* // timestamp=... level=INFO fiber=#0 message="task2 done"
|
|
9666
9851
|
* // hello
|
|
9667
9852
|
* ```
|
|
9668
9853
|
*
|
|
9854
|
+
* @see {@link zipLeft} for a version that returns the result of the first
|
|
9855
|
+
* effect.
|
|
9856
|
+
*
|
|
9669
9857
|
* @since 2.0.0
|
|
9670
9858
|
* @category Zipping
|
|
9671
9859
|
*/
|
|
@@ -9682,9 +9870,9 @@ export const zipRight = fiberRuntime.zipRightOptions;
|
|
|
9682
9870
|
* this function processes the results with a custom function to produce a
|
|
9683
9871
|
* single output.
|
|
9684
9872
|
*
|
|
9685
|
-
*
|
|
9873
|
+
* **Example** (Combining Effects with a Custom Function)
|
|
9874
|
+
*
|
|
9686
9875
|
* ```ts
|
|
9687
|
-
* // Title: Combining Effects with a Custom Function
|
|
9688
9876
|
* import { Effect } from "effect"
|
|
9689
9877
|
*
|
|
9690
9878
|
* const task1 = Effect.succeed(1).pipe(
|
|
@@ -9703,7 +9891,7 @@ export const zipRight = fiberRuntime.zipRightOptions;
|
|
|
9703
9891
|
* (number, string) => number + string.length
|
|
9704
9892
|
* )
|
|
9705
9893
|
*
|
|
9706
|
-
*
|
|
9894
|
+
* Effect.runPromise(task3).then(console.log)
|
|
9707
9895
|
* // Output:
|
|
9708
9896
|
* // timestamp=... level=INFO fiber=#3 message="task1 done"
|
|
9709
9897
|
* // timestamp=... level=INFO fiber=#2 message="task2 done"
|
|
@@ -9791,9 +9979,8 @@ export const withTracerScoped = fiberRuntime.withTracerScoped;
|
|
|
9791
9979
|
/**
|
|
9792
9980
|
* Disable the tracer for the given Effect.
|
|
9793
9981
|
*
|
|
9794
|
-
*
|
|
9795
|
-
*
|
|
9796
|
-
* @example
|
|
9982
|
+
* **Example**
|
|
9983
|
+
*
|
|
9797
9984
|
* ```ts
|
|
9798
9985
|
* import { Effect } from "effect"
|
|
9799
9986
|
*
|
|
@@ -9803,6 +9990,9 @@ export const withTracerScoped = fiberRuntime.withTracerScoped;
|
|
|
9803
9990
|
* Effect.withTracerEnabled(false)
|
|
9804
9991
|
* )
|
|
9805
9992
|
* ```
|
|
9993
|
+
*
|
|
9994
|
+
* @since 2.0.0
|
|
9995
|
+
* @category Tracing
|
|
9806
9996
|
*/
|
|
9807
9997
|
export const withTracerEnabled = core.withTracerEnabled;
|
|
9808
9998
|
/**
|
|
@@ -9913,9 +10103,8 @@ export const withSpan = effect.withSpan;
|
|
|
9913
10103
|
/**
|
|
9914
10104
|
* Wraps a function that returns an effect with a new span for tracing.
|
|
9915
10105
|
*
|
|
9916
|
-
*
|
|
9917
|
-
*
|
|
9918
|
-
* @example
|
|
10106
|
+
* **Example**
|
|
10107
|
+
*
|
|
9919
10108
|
* ```ts
|
|
9920
10109
|
* import { Effect } from "effect"
|
|
9921
10110
|
*
|
|
@@ -9927,6 +10116,9 @@ export const withSpan = effect.withSpan;
|
|
|
9927
10116
|
* })
|
|
9928
10117
|
* })
|
|
9929
10118
|
* ```
|
|
10119
|
+
*
|
|
10120
|
+
* @since 3.2.0
|
|
10121
|
+
* @category Tracing
|
|
9930
10122
|
*/
|
|
9931
10123
|
export const functionWithSpan = effect.functionWithSpan;
|
|
9932
10124
|
/**
|
|
@@ -9968,7 +10160,8 @@ export const withParentSpan = effect.withParentSpan;
|
|
|
9968
10160
|
* and you want to ensure that only non-null values are processed. It helps
|
|
9969
10161
|
* enforce null-safety and makes error handling more explicit.
|
|
9970
10162
|
*
|
|
9971
|
-
*
|
|
10163
|
+
* **Example**
|
|
10164
|
+
*
|
|
9972
10165
|
* ```ts
|
|
9973
10166
|
* import { Effect } from "effect"
|
|
9974
10167
|
*
|
|
@@ -9976,7 +10169,7 @@ export const withParentSpan = effect.withParentSpan;
|
|
|
9976
10169
|
* // ▼
|
|
9977
10170
|
* const maybe1 = Effect.fromNullable(1)
|
|
9978
10171
|
*
|
|
9979
|
-
*
|
|
10172
|
+
* Effect.runPromiseExit(maybe1).then(console.log)
|
|
9980
10173
|
* // Output:
|
|
9981
10174
|
* // { _id: 'Exit', _tag: 'Success', value: 1 }
|
|
9982
10175
|
*
|
|
@@ -9984,7 +10177,7 @@ export const withParentSpan = effect.withParentSpan;
|
|
|
9984
10177
|
* // ▼
|
|
9985
10178
|
* const maybe2 = Effect.fromNullable(null as number | null)
|
|
9986
10179
|
*
|
|
9987
|
-
*
|
|
10180
|
+
* Effect.runPromiseExit(maybe2).then(console.log)
|
|
9988
10181
|
* // Output:
|
|
9989
10182
|
* // {
|
|
9990
10183
|
* // _id: 'Exit',
|
|
@@ -10024,7 +10217,8 @@ export const fromNullable = effect.fromNullable;
|
|
|
10024
10217
|
* It’s ideal for scenarios where you want to explicitly represent optionality
|
|
10025
10218
|
* in a type-safe way while retaining other failure information.
|
|
10026
10219
|
*
|
|
10027
|
-
*
|
|
10220
|
+
* **Example**
|
|
10221
|
+
*
|
|
10028
10222
|
* ```ts
|
|
10029
10223
|
* import { Effect } from "effect"
|
|
10030
10224
|
*
|
|
@@ -10036,7 +10230,7 @@ export const fromNullable = effect.fromNullable;
|
|
|
10036
10230
|
* // ▼
|
|
10037
10231
|
* const option1 = Effect.optionFromOptional(maybe1)
|
|
10038
10232
|
*
|
|
10039
|
-
*
|
|
10233
|
+
* Effect.runPromise(option1).then(console.log)
|
|
10040
10234
|
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
10041
10235
|
*
|
|
10042
10236
|
* // ┌─── Effect<number, NoSuchElementException, never>
|
|
@@ -10047,7 +10241,7 @@ export const fromNullable = effect.fromNullable;
|
|
|
10047
10241
|
* // ▼
|
|
10048
10242
|
* const option2 = Effect.optionFromOptional(maybe2)
|
|
10049
10243
|
*
|
|
10050
|
-
*
|
|
10244
|
+
* Effect.runPromise(option2).then(console.log)
|
|
10051
10245
|
* // Output: { _tag: 'None' }
|
|
10052
10246
|
* ```
|
|
10053
10247
|
*
|
|
@@ -10065,7 +10259,8 @@ export const optionFromOptional = effect.optionFromOptional;
|
|
|
10065
10259
|
* will immediately succeed with a `None` value. If the `Option` is `Some`, the
|
|
10066
10260
|
* inner `Effect` will be executed, and its result wrapped in a `Some`.
|
|
10067
10261
|
*
|
|
10068
|
-
*
|
|
10262
|
+
* **Example**
|
|
10263
|
+
*
|
|
10069
10264
|
* ```ts
|
|
10070
10265
|
* import { Effect, Option } from "effect"
|
|
10071
10266
|
*
|
|
@@ -10129,7 +10324,8 @@ const makeTagProxy = TagClass => {
|
|
|
10129
10324
|
* method) are turned into static properties of the Notifications class, making
|
|
10130
10325
|
* it easier to access them.
|
|
10131
10326
|
*
|
|
10132
|
-
*
|
|
10327
|
+
* **Example**
|
|
10328
|
+
*
|
|
10133
10329
|
* ```ts
|
|
10134
10330
|
* import { Effect } from "effect"
|
|
10135
10331
|
*
|
|
@@ -10182,7 +10378,8 @@ export const Tag = id => () => {
|
|
|
10182
10378
|
* provided automatically when the service is used. Accessors can be optionally
|
|
10183
10379
|
* generated for the service, making it more convenient to use.
|
|
10184
10380
|
*
|
|
10185
|
-
*
|
|
10381
|
+
* **Example**
|
|
10382
|
+
*
|
|
10186
10383
|
* ```ts
|
|
10187
10384
|
* import { Effect } from 'effect';
|
|
10188
10385
|
*
|