title: Scala Cats Effects - IO Async Shift - How Does it Work?
body: <p>Here is some Scala cats code using the <a href="https://github.com/typelevel/cats-effect" rel="noreferrer">IO Monad</a>:</p>
  
  <pre><code>import java.util.concurrent.{ExecutorService, Executors}
  
  import cats.effect.IO
  
  import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
  import scala.util.control.NonFatal
  
  object Program extends App {
  
    type CallbackType = (Either[Throwable, Unit]) =&gt; Unit
  
    // IO.async[Unit] is like a Future that returns Unit on completion.
    // Unlike a regular Future, it doesn't start to run until unsafeRunSync is called.
    def forkAsync(toRun: () =&gt; Unit)(executor: ExecutorService): IO[Unit] = IO.async[Unit] { callback: CallbackType =&gt;
      // "callback" is a function that either takes a throwable (Left) or whatever toRun returns (Right).
      println("LalalaAsync: " + Thread.currentThread().getName)
      executor.execute(new Runnable {
        def run(): Unit = {
          val nothing: Unit = toRun() // Note: This line executes the body and returns nothing, which is of type Unit.
          try {
            callback(Right(nothing)) // On success, the callback returns nothing
          } catch {
            case NonFatal(t) =&gt; callback(Left(t)) // On failure, it returns an exception
          }
        }
      })
    }
  
    def forkSync(toRun: () =&gt; Unit)(executor: ExecutorService): IO[Unit] = IO.apply {
      println("LalalaSync: " + Thread.currentThread().getName)
      executor.execute(new Runnable {
        def run(): Unit = {
          toRun()
        }
      })
    }
  
    val treadPool: ExecutorService = Executors.newSingleThreadExecutor()
    val mainThread: Thread = Thread.currentThread()
  
    val Global: ExecutionContextExecutor = ExecutionContext.global
  
    /*
    Output:
      1 Hello World printed synchronously from Main.main
      LalalaSync: scala-execution-context-global-12
      Hello World printed synchronously from thread pool.pool-1-thread-1
      LalalaAsync: scala-execution-context-global-12
      Hello World printed asynchronously from thread pool.pool-1-thread-1
      2 Hello World printed synchronously from Global .scala-execution-context-global-12
     */
    val program = for {
      _ &lt;- IO {
        println("1 Hello World printed synchronously from Main." + Thread.currentThread().getName) // "main" thread
      }
      _ &lt;- IO.shift(Global) // Shift to Global Execution Context
      _ &lt;- forkSync { () =&gt;
        println("Hello World printed synchronously from thread pool." + Thread.currentThread().getName) // "pool-1-thread-1" thread
      }(treadPool)
      _ &lt;- forkAsync { () =&gt;
        println("Hello World printed asynchronously from thread pool." + Thread.currentThread().getName) // "pool-1-thread-1" thread
      }(treadPool)
      _ &lt;- IO.shift(Global) // Shift to Global Execution Context
      _ &lt;- IO {
        println("2 Hello World printed synchronously from Global ." + Thread.currentThread().getName) // "scala-execution-context-global-13" thread
      }
    } yield ()
  
    program.unsafeRunSync()
  }
  </code></pre>
  
  <p>To run it you would need to add:</p>
  
  <pre><code>libraryDependencies ++= Seq(
    "org.typelevel" %% "cats" % "0.9.0",
    "org.typelevel" %% "cats-effect" % "0.3"
  ),
  </code></pre>
  
  <p>To your build.sbt file.</p>
  
  <p>Note the output:</p>
  
  <pre><code>  /*
    Output:
      1 Hello World printed synchronously from Main.main
      LalalaSync: scala-execution-context-global-12
      Hello World printed synchronously from thread pool.pool-1-thread-1
      LalalaAsync: scala-execution-context-global-12
      Hello World printed asynchronously from thread pool.pool-1-thread-1
      2 Hello World printed synchronously from Global .scala-execution-context-global-12
   */
  </code></pre>
  
  <p>Basically, I don't understand how IO.shift(Global) or how IO.async works.</p>
  
  <p>For example, why is it that after I call "forkAsync", if I don't call "IO.shift(Global)", the subsequent synchronous IO objects are run in "pool-1-thread-1". Also, what is the difference between forkAsync and forkSync in this example? Both of them start in the ExecutionContext.global and then execute a Runnable in "pool.pool-1-thread-1". </p>
  
  <p>Like are forkAsync and forkSync doing the exact same thing or is forkAsync doing something different? If they are doing the same thing, what is the point of wrapping code in IO.async? If they are not doing the same thing, how are they different?</p>