如何将 `getOrWaitUntilNonEmpty` 做为单个衬垫?

How to do a `getOrWaitUntilNonEmpty` as a single liner?

我有一个高级代码结构,如下所示:

val block: (=> Option[Seq[String]]) = ...
val matches = block().get.toArray

问题是此代码可能会失败,即 .get 变为 None,具体取决于时间,例如我 page-scraping Google 太频繁了,然后我会等待并重试...

我可以这样等待,即在 11-16 秒之间随机等待:

val random = new Random()
Thread.sleep((11000 * random.nextFloat() + 6000).ceil.toInt)

什么是优雅的单行方式来[等待]循环直到执行block的结果不为空?类似于:

val timeInMillis = (11000 * random.nextFloat() + 6000).ceil.toInt
block().getOrWaitUntilNonEmpty(timeInMillis).toArray

像这样尝试softwaremill/retry

retry.Pause(max = 10, Defaults.delay)(odelay.Timer.default) {
  block
}

有依赖关系

"com.softwaremill.retry"  %% "retry"       % "0.3.2"
"com.softwaremill.odelay" %% "odelay-core" % "0.3.0"