Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8594791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:16:48+00:00 2026-06-12T00:16:48+00:00

I want to make two calls to my database which will take a while

  • 0

I want to make two calls to my database which will take a while to return a result, and I don’t want to block the current thread. I have used Akka Futures to wrap the database calls.

Instead of waiting (blocking) for both calls to return, I would like to specify a callback function to be called, which can then render the response. How do I do that? Here is my controller code:

def showPie = IsAuthenticated(Roles.validator) { user => implicit request =>
    val eventUid = request.session.get(EventUid).get

    val printed = Akka.future(TicketRepository.getCountForState(eventUid, "Printed"))
    val validated = Akka.future(TicketRepository.getCountForState(eventUid, "Validated"))

    //this would be evil, because it would block: Ok(views.html.pie(printed.await(1000).get, validated.await(1000).get)) 

    //create a promise for all the promised results
    val promise = Promise.sequence(List(printed, validated))

    //this doesnt work, but how can I make it work WITHOUT blocking this thread?
    promise.callWhenResultIsReady(Ok(view.html.pie(promise.get))
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:16:49+00:00Added an answer on June 12, 2026 at 12:16 am

    You’re close. You can simple call map on a promise to deal with it. Inside an Async block, it stays nonblocking. Relevant documentation (see “AsyncResult”).

    def showPie = IsAuthenticated(Roles.validator) { user => implicit request =>
        val eventUid = request.session.get(EventUid).get
    
        val printed = Akka.future(TicketRepository.getCountForState(eventUid, "Printed"))
        val validated = Akka.future(TicketRepository.getCountForState(eventUid, "Validated"))
    
        //create a promise for all the promised results
        val promise = Promise.sequence(List(printed, validated))
        Async {
            promise map { res =>
                Ok("Got it!" + res)
            }
        }
    }
    

    edit:
    From your comment below, let’s take a closer look at the Async block. Async takes a Promise, and returns an AsyncResult, which is a subtype of Result (which is what Action needs).

        Async {
            // We take the promise, and add something akin to a callback
            //  function with `map`. This new function is called when `promise`
            //  is complete.
            val result = promise map { res => // this is the redeemed promise
              Ok("Got it!" + res)
            }
            result // this is the new promise
        } 
    

    Since the map function is called when promise is complete, this stays non-blocking. This whole block returns quickly with an AsyncResult, and Play! manages it in a similar fashion by returning to the client when it finishes (and freeing Play! to do other things in the meantime).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make two block stand side by side. In Opera, Chrome and
I want to make a number selector like in alarm application, which has two
I want to make two tables on single screen, so that if Table A
I want to make a single figure in R with two plots in a
Lets say I want to make a class myClass with two slots A and
I have these two URL's I want to make available to my users: /Account/Orders
i have two strings in a php i want to make following checks in
I have two displays. I want to make a media player and I want
I have two QTreeWidgetItem that are top level. I want to make the second
This is a two part question, but I want to make sure the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.