You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala中能否在guard代码块内等待Future完成?

Waiting for a Future Inside a Scala Match Guard

Hey there! Let's break down how to handle your request—waiting for a Future to complete directly inside a match guard, without putting logic in the case body or outside the Try/match block.

The Short Answer

You can use Await.result to block and get the Future's result right in the guard. This works because match guards expect a boolean value, and Await.result will block the current thread until the Future finishes, returning its boolean outcome.

Working Code Example

import scala.util.Try
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.duration.Duration.Inf

object Main extends App {
  val f = Future { Thread.sleep(3000); true }
  Try(1) match {
    case x if Await.result(f, Inf) => println("Waited for future") // Guard blocks until Future completes
    case _ => println(":(")
  }
}

Key Details

  • Why this fits your constraints: All the logic to wait for the Future lives entirely inside the guard condition—no code is moved into the case body or outside the Try/match structure.
  • The catch with blocking: Using Await.result (especially with an infinite timeout like Inf) is safe for small scripts or testing, but in production systems (like Akka/Play apps), blocking threads can drain your thread pool and cause performance issues or deadlocks. Use this cautiously in real-world code.
  • Why your original code didn't work: Your line case x if f tries to use a Future[Boolean] as a boolean value, which Scala won't allow—you need to extract the actual boolean result from the Future first, which Await.result does.

Why Async Callbacks Won't Work Here

Match guards run synchronously. If you tried using something like f.onComplete, the callback would run on a separate thread long after the guard has already evaluated. There's no way to pause the guard's execution to wait for an async callback to finish, so blocking is the only viable path here.

内容的提问来源于stack exchange,提问作者Oleg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:58:09