scala/scala-async

Awaiting alternatives for asynchronous operation?

denyshorman opened this issue · 1 comments

What do you think about providing alternatives for awaiting asynchronous operation?

Typical async/await code looks like that:

async {
    val v1 = await(Db.query("select 1").fetchOne[Int])
    val v2 = await(Db.query("select 2").fetchOne[Int])
    v1 + v2
}

It would great to have a choise how to await. For example, you can await Future[T] using prefix operator ! or postfix operator await.

Awaiting with prefix ! operator:

async {
    val v1 = !Db.query("select 1").fetchOne[Int]
    val v2 = !Db.query("select 2").fetchOne[Int]
    v1 + v2
}

Awaiting with postfix await operator:

async {
    val v1 = Db.query("select 1").fetchOne[Int].await
    val v2 = Db.query("select 2").fetchOne[Int].await
    v1 + v2
}

and resulting code looks a little bit shorter without parentheses.
What do you think about this idea ?

I'd rather not provide more than one way to do this in the library itself, but you are able and welcome to introduce a macro into your own project that expands to the standard await idiom. In fact, 2014-era @retronym demoed such a thing in another ticket here 😄