I am using a function from third party code, which looks like this:
scala> def willCallback(fun: Function1[Int, Unit]) {
| doWork()
| fun(1)
| }
willCallback: (fun: Int => Unit)Unit
In my code, I define a function and would like it to return the callback function parameter, to achieve this:
scala> def callbackResult():Int = {
| willCallback( (i:Int) => {
| // What do I put here
| // to make the return value of callbackResult to be i?
| })
| }
What can I do to make it work?
Thanks.
From your comment it seems that you want
willCallbackto block until the callback has executed. Here’s how you can do it with theFuture/PromiseAPI in Scala 2.10,Of course, there is the danger that if the callback never executes, your
callbackResult()method will hang indefinitely. To avoid this danger, it might be better forcallbackResult()to returnf: Future[Int]rather than await itsIntvalue, as Régis Jean-Gilles suggests.