Course map
miso // 14

IO in update

Read

update never runs IO, it schedules it

The update function is pure: it describes changes. To talk to the outside world (log to the console, wait, fetch) it schedules an IO action with io or io_. miso runs it after update returns.

data Action = Ping | Pong | Log MisoString

update :: Action -> Effect () () Int Action
update Ping = do
  this += 1
  io_ (consoleLog "ping!")        -- IO (), no result
  io (threadDelay 1000000 >> pure Pong)   -- IO Action
update Pong = this -= 1
update (Log s) = io_ (consoleLog s)

io_ takes an IO () and forgets the result. io takes an IO Action: when it finishes, the action it produced is dispatched. Here Pong arrives one second after Ping. threadDelay is from Control.Concurrent and takes microseconds.

01 / 04