当我制作future
或应用方法如onSuccess
和map
时,我可以为它们指定ExecutionContext。Scala:用于将来理解的ExecutionContext
例如,
val f = future {
// code
} executionContext
f.map(someFunction)(executionContext)
f onSuccess {
// code
} executionContext
但是,如果我用一个换理解未来的,我怎么能为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
而且,如果没有指定,ExecutionContext在yield中运行代码?
编辑
确定。感谢答案,我找到了一些东西。
如果我没有定义或导入隐含的ExecutionContext(如Implicits.global
), 的换理解不编译。这意味着,for-comprehension使用隐式的ExecutionContext。
然后,我该如何使用不含隐式ExecutionContext的理解,即如何指定?
请参阅我的回答,了解为什么如果您没有指定“隐式”,for'comprehensions将无法编译。 –
@flavian在简单的情况下,这是有效的。但是如果有两个ExecutionContext用于理解,我该如何指定?它会显示错误,如“含糊的隐含值”。我可以阻止每次我定义或导入隐式vals或defs&for-comprehension,但有没有另一种方式? – Naetmul
只是要绝对确定 - 你的意思是用多个生成器(在这种情况下,flavian的答案是有效的)还是多个相应的for-comprehensions一个'for for'-理解? –