Skip to content

Commit

Permalink
Fixed the stupid exception code. Every part of this code which looks
Browse files Browse the repository at this point in the history
nice is proposed by Tiark. Many thanks!
  • Loading branch information
danielkroeni committed Feb 17, 2012
1 parent 11b9549 commit 134d7d8
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/main/scala/async/Async.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,9 @@ object Async {

def await[A](block: => Future[A]): A@suspendable = {
val ec = localContext get()
val ex = new AtomicReference[Throwable] // holds the exception if any
val a = shift[A,Unit,Unit] { cont: (A => Unit) =>
block onComplete {
case Right(r) => ec.execute(cont(r))
case Left(e) =>
ex.set(e) // store exception
ec.execute(cont(null.asInstanceOf[A])) //TODO solve appropriately
}
val a = shift[Either[Throwable,A],Unit,Unit] { cont: (Either[Throwable,A] => Unit) =>
block onComplete { r => ec.execute(cont(r)) }
}
// rethrow exception if any
if(ex.get() != null) throw ex.get()
a
a.fold(throw _, x => x)
}
}

0 comments on commit 134d7d8

Please sign in to comment.