A program is a valueLesson 6 of 23

Who can stop it?

Run work side by side, and watch the loser of a race clean up.

download pretends to fetch a file. It logs when it starts and when it's done, and it takes as long as you tell it to. The three in program take 400, 600 and 500 milliseconds.

Predict

Effect.all takes a list of Effects and gives back a list of their results. Each output line shows when it was printed. At about what time does the last done appear?

Check your answer

At about 1500 milliseconds. By default Effect.all runs them one after another, like the steps in lesson 2, so the times add up. Press Run and read the left column: b doesn't start until a is done.

Try it

  1. Pass a second argument to Effect.all: { concurrency: "unbounded" }. Before you run: when does the last done print now, and in what order do the three finish?

  2. Turn program into an Effect.gen. Make what you have its first step, const all = yield* Effect.all(...), then add a second step and return both:

    const winner = yield* Effect.race(
      download("fast", 300),
      download("slow", 900)
    )
    return { all, winner }

    race gives you whichever finishes first. Before you run: how many done lines will these two print between them?

  3. One of the lines says slow: interrupted. Find the code that prints it. Does download ever check whether it should stop?

  4. Delete the Effect.onInterrupt lines from download and run again. Nothing announces an interruption now. So is slow still stopped, or does it run on to its done?

About steps 3 and 4

download never checks anything. It was stopped from outside, in the middle of its sleep, and onInterrupt is cleanup that runs when that happens.

Without those lines it still stops: slow: done never prints. It just doesn't say so any more. The stopping was never download's job.

What you found

Something runs your descriptions, and Effect calls it the runtime. It runs each piece of work on a fiber, which is a very light thread that it manages. concurrency: "unbounded" asked for three fibers at once, so the three downloads took as long as the slowest instead of the sum.

Because the runtime is what runs the work, it can also stop it. race interrupts the loser as soon as there is a winner. timeout, from lesson 3, is the same thing with a clock as the opponent. The work stops for real, at its next step, and any cleanup it registered runs first.

Compare a call that has already gone out and can't be taken back. You can ignore its answer, but it keeps going. Here cancelling is part of how programs run, so nobody has to remember to write it.

A download answers once and ends. A clock doesn't, and neither does a socket or a keyboard. What if the answers keep coming?

06
Principle 6

Whoever runs it can stop it

A runtime runs your description, so it can also interrupt it, and cleanup runs either way. Cancelling isn't something you have to remember to write.

OutputNot run yet

Not run yet

Read the code and say what it will do. Then press Run.