Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Await.ready and Await.result

Tags:

scala

future

I know this is quite an open ended question and I apologize.

I can see that Await.ready returns Awaitable.type while Await.result returns T but I still confuse them.

What are the difference between the two?

Is one blocking and the other one non-blocking?

like image 287
octavian Avatar asked Dec 15 '16 17:12

octavian


1 Answers

They both block until the future completes, the difference is just their return type.

The difference is useful when your Future throws exceptions:

def a = Future { Thread.sleep(2000); 100 } def b = Future { Thread.sleep(2000); throw new NullPointerException }  Await.ready(a, Duration.Inf) // Future(Success(100))     Await.ready(b, Duration.Inf) // Future(Failure(java.lang.NullPointerException))  Await.result(a, Duration.Inf) // 100 Await.result(b, Duration.Inf) // crash with java.lang.NullPointerException 
like image 144
soote Avatar answered Sep 18 '22 19:09

soote