Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

Scala Future flatMap implementation (chaining)

Trying to wrap my head around how async tasks are chained together, for ex futures and flatMap

val a: Future[Int] = Future { 123 }
val b: Future[Int] = a.flatMap(a => Future { a + 321 })

How would i implement something similar where i build a new Future by waiting for a first and applying a f on the result. I tried looking a scala's source code but got stuck here: https://github.com/scala/scala/blob/2.13.x/src/library/scala/concurrent/Future.scala#L216

i would imagine some code like:

def myFlatMap(a: Future[Int])(f: a => Future[Int]): Future[Int] = {
   Future {
      // somehow wait for a to complete and the apply 'f'
      a.onComplete {
        case Success(x) => f(a)
      }
   }
}

but i guess above will return a Future[Unit] instead. I would just like to understand the "pattern" of how async tasks are chained together.

question from:https://stackoverflow.com/questions/66052233/scala-future-flatmap-implementation-chaining

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(Disclaimer: I'm the main maintainer of Scala Futures)

You wouldn't want to implement flatMap/recoverWith/transformWith yourself—it is a very complex feature to implement safely (stack-safe, memory-safe, and concurrency-safe).

You can see what I am talking about here:

There is more reading on the topic here: https://viktorklang.com/blog/Futures-in-Scala-2.12-part-9.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...