In this guide, we will explore the differences between Promise
and Effect
, two approaches to handling asynchronous operations in TypeScript. We’ll discuss their type safety, creation, chaining, and concurrency, providing examples to help you understand their usage.
Comparing Effects and Promises: Key Distinctions
Evaluation Strategy: Promises are eagerly evaluated, whereas effects are lazily evaluated.
Execution Mode: Promises are one-shot, executing once, while effects are multi-shot, repeatable.
Interruption Handling and Automatic Propagation: Promises lack built-in interruption handling, posing challenges in managing interruptions, and don’t automatically propagate interruptions, requiring manual abort controller management. In contrast, effects come with interruption handling capabilities and automatically compose interruption, simplifying management locally on smaller computations without the need for high-level orchestration.
Structured Concurrency: Effects offer structured concurrency built-in, which is challenging to achieve with Promises.
Error Reporting (Type Safety): Promises don’t inherently provide detailed error reporting at the type level, whereas effects do, offering type-safe insight into error cases.
Runtime Behavior: The Effect runtime aims to remain synchronous as long as possible, transitioning into asynchronous mode only when necessary due to computation requirements or main thread starvation.
Let’s start by comparing the types of Promise
and Effect
. The type parameter A
represents the resolved value of the operation:
Effect < A , Error, Context >
Here’s what sets Effect
apart:
It allows you to track the types of errors statically through the type parameter Error
. For more information about error management in Effect
, see Expected Errors .
It allows you to track the types of required dependencies statically through the type parameter Context
. For more information about context management in Effect
, see Managing Services .
Let’s compare creating a successful operation using Promise
and Effect
:
const success = Promise . resolve ( 2 )
import { Effect } from "effect"
const success = Effect . succeed ( 2 )
Now, let’s see how to handle failures with Promise
and Effect
:
const failure = Promise . reject ( "Uh oh!" )
import { Effect } from "effect"
const failure = Effect . fail ( "Uh oh!" )
Creating operations with custom logic:
const task = new Promise < number >(( resolve , reject ) => {
Math . random () > 0.5 ? resolve ( 2 ) : reject ( "Uh oh!" )
import { Effect } from "effect"
const task = Effect . gen ( function* () {
yield* Effect . sleep ( "300 millis" )
return Math . random () > 0.5 ? 2 : yield* Effect . fail ( "Uh oh!" )
Mapping the result of an operation:
const mapped = Promise . resolve ( "Hello" ). then (( s ) => s . length )
import { Effect } from "effect"
const mapped = Effect . succeed ( "Hello" ). pipe (
Effect . map (( s ) => s . length )
// or Effect.andThen((s) => s.length)
Chaining multiple operations:
const flatMapped = Promise . resolve ( "Hello" ). then (( s ) =>
Promise . resolve ( s . length )
import { Effect } from "effect"
const flatMapped = Effect . succeed ( "Hello" ). pipe (
Effect . flatMap (( s ) => Effect . succeed ( s . length ))
// or Effect.andThen((s) => Effect.succeed(s.length))
Comparing Effect.gen with async/await
If you are familiar with async
/await
, you may notice that the flow of writing code is similar.
Let’s compare the two approaches:
const increment = ( x : number ) => x + 1
const divide = ( a : number , b : number ) : Promise < number > =>
? Promise . reject ( new Error ( "Cannot divide by zero" ))
const task1 = Promise . resolve ( 10 )
const task2 = Promise . resolve ( 2 )
const program = async function () {
const n1 = await divide ( a , b )
return `Result is: ${ n2 }`
program (). then ( console . log ) // Output: "Result is: 6"
import { Effect } from "effect"
const increment = ( x : number ) => x + 1
const divide = ( a : number , b : number ) : Effect . Effect < number , Error > =>
? Effect . fail ( new Error ( "Cannot divide by zero" ))
const task1 = Effect . promise (() => Promise . resolve ( 10 ))
const task2 = Effect . promise (() => Promise . resolve ( 2 ))
const program = Effect . gen ( function* () {
const n1 = yield* divide ( a , b )
return `Result is: ${ n2 }`
Effect . runPromise ( program ). then ( console . log )
// Output: "Result is: 6"
It’s important to note that although the code appears similar, the two programs are not identical. The purpose of comparing them side by side is just to highlight the resemblance in how they are written.
const task1 = new Promise < number >(( resolve , reject ) => {
console . log ( "Executing task1..." )
console . log ( "task1 done" )
const task2 = new Promise < number >(( resolve , reject ) => {
console . log ( "Executing task2..." )
console . log ( "task2 done" )
const task3 = new Promise < number >(( resolve , reject ) => {
console . log ( "Executing task3..." )
console . log ( "task3 done" )
const program = Promise . all ([ task1 , task2 , task3 ])
program . then ( console . log , console . error )
import { Effect } from "effect"
const task1 = Effect . gen ( function* () {
console . log ( "Executing task1..." )
yield* Effect . sleep ( "100 millis" )
console . log ( "task1 done" )
const task2 = Effect . gen ( function* () {
console . log ( "Executing task2..." )
yield* Effect . sleep ( "200 millis" )
console . log ( "task2 done" )
return yield* Effect . fail ( "Uh oh!" )
const task3 = Effect . gen ( function* () {
console . log ( "Executing task3..." )
yield* Effect . sleep ( "300 millis" )
console . log ( "task3 done" )
const program = Effect . all ([ task1 , task2 , task3 ], {
Effect . runPromise ( program ). then ( console . log , console . error )
(FiberFailure) Error: Uh oh!
const task1 = new Promise < number >(( resolve , reject ) => {
console. log ( "Executing task1..." )
console. log ( "task1 done" )
const task2 = new Promise < number >(( resolve , reject ) => {
console. log ( "Executing task2..." )
console. log ( "task2 done" )
const task3 = new Promise < number >(( resolve , reject ) => {
console. log ( "Executing task3..." )
console. log ( "task3 done" )
const program = Promise . allSettled ([task1, task2, task3])
program. then (console.log, console.error)
{ status: 'fulfilled', value: 1 },
{ status: 'rejected', reason: 'Uh oh!' },
{ status: 'fulfilled', value: 3 }
import { Effect } from "effect"
const task1 = Effect . gen ( function* () {
console . log ( "Executing task1..." )
yield* Effect . sleep ( "100 millis" )
console . log ( "task1 done" )
const task2 = Effect . gen ( function* () {
console . log ( "Executing task2..." )
yield* Effect . sleep ( "200 millis" )
console . log ( "task2 done" )
return yield* Effect . fail ( "Uh oh!" )
const task3 = Effect . gen ( function* () {
console . log ( "Executing task3..." )
yield* Effect . sleep ( "300 millis" )
console . log ( "task3 done" )
const program = Effect . forEach (
( task ) => Effect . either ( task ), // or Effect.exit
Effect . runPromise ( program ). then ( console . log , console . error )
const task1 = new Promise < number >(( resolve , reject ) => {
console. log ( "Executing task1..." )
console. log ( "task1 done" )
reject ( "Something went wrong!" )
const task2 = new Promise < number >(( resolve , reject ) => {
console. log ( "Executing task2..." )
console. log ( "task2 done" )
const task3 = new Promise < number >(( resolve , reject ) => {
console. log ( "Executing task3..." )
console. log ( "task3 done" )
const program = Promise . any ([task1, task2, task3])
program. then (console.log, console.error)
import { Effect } from "effect"
const task1 = Effect . gen ( function* () {
console . log ( "Executing task1..." )
yield* Effect . sleep ( "100 millis" )
console . log ( "task1 done" )
return yield* Effect . fail ( "Something went wrong!" )
const task2 = Effect . gen ( function* () {
console . log ( "Executing task2..." )
yield* Effect . sleep ( "200 millis" )
console . log ( "task2 done" )
const task3 = Effect . gen ( function* () {
console . log ( "Executing task3..." )
yield* Effect . sleep ( "300 millis" )
console . log ( "task3 done" )
return yield* Effect . fail ( "Uh oh!" )
const program = Effect . raceAll ([ task1 , task2 , task3 ])
Effect . runPromise ( program ). then ( console . log , console . error )
const task1 = new Promise < number >(( resolve , reject ) => {
console . log ( "Executing task1..." )
console . log ( "task1 done" )
reject ( "Something went wrong!" )
const task2 = new Promise < number >(( resolve , reject ) => {
console . log ( "Executing task2..." )
console . log ( "task2 done" )
const task3 = new Promise < number >(( resolve , reject ) => {
console . log ( "Executing task3..." )
console . log ( "task3 done" )
const program = Promise . race ([ task1 , task2 , task3 ])
program . then ( console . log , console . error )
import { Effect } from "effect"
const task1 = Effect . gen ( function* () {
console . log ( "Executing task1..." )
yield* Effect . sleep ( "100 millis" )
console . log ( "task1 done" )
return yield* Effect . fail ( "Something went wrong!" )
const task2 = Effect . gen ( function* () {
console . log ( "Executing task2..." )
yield* Effect . sleep ( "200 millis" )
console . log ( "task2 done" )
return yield* Effect . fail ( "Uh oh!" )
const task3 = Effect . gen ( function* () {
console . log ( "Executing task3..." )
yield* Effect . sleep ( "300 millis" )
console . log ( "task3 done" )
const program = Effect . raceAll ([ task1 , task2 , task3 ]. map ( Effect . either )) // or Effect.exit
Effect . runPromise ( program ). then ( console . log , console . error )
left: "Something went wrong!"
Question . What is the equivalent of starting a promise without immediately waiting for it in Effects?
const task = ( delay : number , name : string ) =>
console . log ( `${ name } done` )
export async function program () {
const r0 = task ( 2_000 , "long running task" )
const r1 = await task ( 200 , "task 2" )
const r2 = await task ( 100 , "task 3" )
program (). then ( console . log )
{ r1: 'task 2', r2: 'task 3', r0: 'long running promise' }
Answer: You can achieve this by utilizing Effect.fork
and Fiber.join
.
import { Effect , Fiber } from "effect"
const task = ( delay : number , name : string ) =>
Effect . gen ( function* () {
yield* Effect . sleep ( delay )
console . log ( `${ name } done` )
const program = Effect . gen ( function* () {
const r0 = yield* Effect . fork ( task ( 2_000 , "long running task" ))
const r1 = yield* task ( 200 , "task 2" )
const r2 = yield* task ( 100 , "task 3" )
r0 : yield* Fiber . join ( r0 )
Effect . runPromise ( program ). then ( console . log )
{ r1: 'task 2', r2: 'task 3', r0: 'long running promise' }