When does it run?
The difference between doing something and describing it.
Before any library, look at three lines of ordinary code. Each one mentions a greeting.
Predict
Read main.ts without running it. Lines a, b and c each contain a console.log. When this file runs, which of the three greetings print?
Check your answer
Only A.
Line a calls console.log on the spot, so it prints the moment the file loads. Line b makes a function, and nobody calls it. Line c makes an Effect, and nobody runs it.
Press Run to see it. The other line of output comes from program, which you'll get to in a moment.
Try it
Add
b()on a line of its own, twice. Before you run: how many times will B print, and how many times will A?bisn't a greeting. So what is it? Say it in your own words before reading on.Now the Effect. Change the last line to
export const program = c. What prints, and how often?Make
programrunctwice:c.pipe(Effect.andThen(c)). Did writing that line print anything by itself?
About step 2
b is a description of a greeting: a value that says how to greet, without greeting. Calling it is what makes the greeting happen, and you can call it as often as you like. a is different. It already happened, once, and all that's left of it is undefined.
What you found
There are two kinds of line here. One does something. The other describes something that can be done later, by whoever holds the description.
Effect.sync(() => ...) is the second kind, like the function. c is a value. Nothing happened when it was made, and nothing happened when you combined it with itself. Things happened only when something ran it. Here that something is the sandbox, which runs the Effect you export as program. In an app it is one call at the very edge, such as Effect.runPromise(program).
So far an Effect looks like a function with extra steps. The difference is what you can do with the description before it runs, and that takes the next few lessons. Start with the simplest thing: you have two descriptions, and you want one to happen after the other.
Describe, then run
An Effect is a description of work. Nothing happens until something runs it, so the same description can be run once, many times or never.