Skip to content

Instantly share code, notes, and snippets.

@agolovenko
Last active April 6, 2022 17:52
Show Gist options
  • Save agolovenko/c55d60860248f6eb0a1180f1115a4fac to your computer and use it in GitHub Desktop.
Save agolovenko/c55d60860248f6eb0a1180f1115a4fac to your computer and use it in GitHub Desktop.
Future with delayed execution in a non-blocking style via akka scheduler
import akka.actor.Scheduler
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{ExecutionContext, Future, Promise}
object DelayedFuture {
def apply[T](scheduler: Scheduler, delay: FiniteDuration)(body: => T)(implicit ec: ExecutionContext): Future[T] = {
val delayPromise = Promise[Unit]()
scheduler.scheduleOnce(delay)(delayPromise.success(()))
delayPromise.future.map { _ => body }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment