Last active
April 6, 2022 17:52
-
-
Save agolovenko/c55d60860248f6eb0a1180f1115a4fac to your computer and use it in GitHub Desktop.
Future with delayed execution in a non-blocking style via akka scheduler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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