Scala 重新尝试未完成的未来
给定此函数,它将重试未计算的Scala 重新尝试未完成的未来,scala,Scala,给定此函数,它将重试未计算的未来n次: scala> def retry[A](x: Function0[Future[A]], n: Int): Future[A] = { | if(n <= 0) | x() | else | println("n" + n) | x().recoverWith({ case _ => retry(x, n - 1) }) |
未来
n次:
scala> def retry[A](x: Function0[Future[A]], n: Int): Future[A] = {
| if(n <= 0)
| x()
| else
| println("n" + n)
| x().recoverWith({ case _ => retry(x, n - 1) })
| }
retry: [A](x: () => scala.concurrent.Future[A], n: Int)scala.concurrent.Future[A]
然后,我调用了重试(g,5)
:
scala>重试[Int](g,5)
n5
n4
res25:scala.concurrent.Future[Int]=Future()
n3
氮气
n1
在获得此输出后,我等待了2分钟,但未来并未显示为已完成:
scala> res25
res28: scala.concurrent.Future[Int] = Future(<not completed>)
scala> res25.value
res29: Option[scala.util.Try[Int]] = None
scala>res25
res28:scala.concurrent.Future[Int]=Future()
scala>res25.value
res29:Option[scala.util.Try[Int]]=None
这是怎么回事?TLDR:如果
其他语句,请始终使用括号
如果
statment有误导性,那么你就是
,以下是括号中的内容:
def retry[A](x: Function0[Future[A]], n: Int): Future[A] = {
if (n <= 0) {
println("n" + n) //added this for clarity
x()
} else {
println("n" + n)
}
x().recoverWith({ case _ => retry(x, n - 1) })
}
TLDR:如果
语句,请始终使用括号
如果
statment有误导性,那么你就是
,以下是括号中的内容:
def retry[A](x: Function0[Future[A]], n: Int): Future[A] = {
if (n <= 0) {
println("n" + n) //added this for clarity
x()
} else {
println("n" + n)
}
x().recoverWith({ case _ => retry(x, n - 1) })
}
def retry[A](x: Function0[Future[A]], n: Int): Future[A] = {
if (n <= 0) {
println("n" + n) //added this for clarity
x()
} else {
println("n" + n)
}
x().recoverWith({ case _ => retry(x, n - 1) })
}
def retry[A](x: Function0[Future[A]], n: Int): Future[A] = {
if (n <= 0) {
println("n" + n)
x()
} else {
println("n" + n)
x().recoverWith({ case _ => retry(x, n - 1) })
}
}