Skip to content

Instantly share code, notes, and snippets.

@dkomanov
Created May 25, 2020 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkomanov/1ab93ca80bf6df97ce4b64e6192eea6a to your computer and use it in GitHub Desktop.
Save dkomanov/1ab93ca80bf6df97ce4b64e6192eea6a to your computer and use it in GitHub Desktop.
[writing-async-app-in-scala-part-3] map described
def map[U](f: T => U)
(implicit ec: ExecutionContext): Future[U] = {
val promise = Promise()
if (isCompleted) // in real code this is an atomic operation + pattern matching
// even if it's completed, callback is submitted to ExecutionContext
ec.execute(() => {
f(result)
})
else
registerCallback(f)(ec)
promise.future
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment