🔴 Scala Beginner

What is a Future in Scala?

Answer

A Scala Future[T] represents a computation that will complete asynchronously with either a value (Success(T)) or an error (Failure(Throwable)). Create: import scala.concurrent.ExecutionContext.Implicits.global; val f = Future { expensiveComputation() }. The computation runs in a thread pool. Combine: f.map(_ * 2), f.flatMap(x => Future { x + 1 }). Handle results: f.onComplete { case Success(v) => ...; case Failure(e) => ... }. Compose multiple: Future.sequence(List(f1, f2, f3)). For-comprehension: for { a <- futureA; b <- futureB } yield a + b. Futures require an implicit ExecutionContext that provides the thread pool. The default global context is suitable for most use cases. For production systems with back pressure requirements, consider using Akka or Cats Effect IO instead of raw Futures.