相关疑难解决方法(0)

Scala:ExecutionContext,用于未来的理解

当我做一个future,或者应用类似的方法onSuccessmap,我能为他们指定的ExecutionContext.

例如,

val f = future {
  // code
} executionContext

f.map(someFunction)(executionContext)

f onSuccess {
  // code
} executionContext
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用了对未来的理解,我该如何为该yield部分指定ExecutionContext ?

for {
  f <- future1
  g <- future2
} yield {
  // code to be executed after future1 onSuccess and future2 onSuccess
  // What ExecutionContext runs this code?
} // (executionContext) here does not work
Run Code Online (Sandbox Code Playgroud)

而且,如果没有指定,ExecutionContext在yield中运行代码是什么?


编辑

好.感谢答案,我找到了一些东西.
如果我没有定义或导入隐式 ExecutionContext(如Implicits.global),则for-comprehension不会编译.这意味着,for-comprehension使用隐式ExecutionContext.

那么,如何在没有隐式ExecutionContext的情况下使用for-comprehension,即如何指定?

scala future for-comprehension executioncontext

7
推荐指数
1
解决办法
3230
查看次数