What's the best practice for async APIs that return futures on Scala?

Posted by Maurício Linhares on Programmers See other posts from Programmers or by Maurício Linhares
Published on 2012-03-10T12:26:09Z Indexed on 2012/11/01 5:16 UTC
Read the original article Hit count: 360

Filed under:
|
|

I have started a project to write an async PostgreSQL driver on Scala and to be async, I need to accept callbacks and use futures, but then accepting a callback and a future makes the code cumbersome because you always have to send a callback even if it is useless.

Here's a test:

"insert a row in the database" in {

  withHandler {
    (handler, future) =>
      future.get(5, TimeUnit.SECONDS)
      handler.sendQuery( this.create ){ query =>  }.get( 5, TimeUnit.SECONDS )
      handler.sendQuery( this.insert ){ query =>  }.get( 5, TimeUnit.SECONDS ).rowsAffected === 1
  }

}

Sending the empty callback is horrible but I couldn't find a way to make it optional or anything like that, so right now I don't have a lot of ideas on how this external API should look like.

It could be something like:

handler.sendQuery( this.create ).addListener { query => println(query) }

But then again, I'm not sure how people are organizing API's in this regard. Providing examples in other projects would also be great.

© Programmers or respective owner

Related posts about design

Related posts about scala