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

  • SEARCH
  • Home
  • 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 8359965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:19:02+00:00 2026-06-09T11:19:02+00:00

I use the Enumerator pattern to retrieve some tweets every second with WS.url Enumerator.fromCallback[String](()

  • 0

I use the Enumerator pattern to retrieve some tweets every second with WS.url

Enumerator.fromCallback[String](() => 
        Promise.timeout(WS.url("http://search.twitter.com/search.json?q="+query+"&rpp=1").get().map { response =>
            val tweets = response.json \\ "text"
            tweets match {
                case Nil => "Nothing found!"
                case head :: tail => query + " : " + head.as[String]
            }
        }.map(Some.apply).value.get, 1000 milliseconds)
  )

My is problem is that

Enumerator.fromCallback[String]() 

is waiting for a

Promise[Option[String]]

As WS.url(…).get returns a Promise, and as I use Promise.timeout to re-launch the call every second,

I have a

Promise[Promise[Option[String]]] 

So I have to use value.get to have the good type, so it does not seems very clean for the asynchronous aspect.

This code works but my question is : Is there a better way, more elegant, to achieve this?
Can I get easily a Promise from another Promise and a Promise.timeout?

Thanks 🙂

  • 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-09T11:19:04+00:00Added an answer on June 9, 2026 at 11:19 am

    Promise is a monad, and in general when you find yourself with a nested monad you want to stick a flatMap in there somewhere. In your case something like this should work:

    import akka.util.duration._
    import play.api.libs.concurrent._
    import play.api.libs.iteratee._
    import play.api.libs.ws._
    
    val query = "test"
    val url = WS.url("http://search.twitter.com/search.json?q=" + query + "&rpp=1")
    
    val tweets = Enumerator.fromCallback[String](() => 
      Promise.timeout(url.get, 1000 milliseconds).flatMap(_.map { response =>
        (response.json \\ "text") match {
          case Nil => "Nothing found!"
          case head :: _ => query + " : " + head.as[String]
        }
      }.map(Some.apply))
    )
    

    I’d personally write it more like this:

    val tweets = Enumerator.fromCallback[String](() => 
      Promise.timeout(url.get, 1000 milliseconds).flatMap(_.map(
        _.json.\\("text").headOption.map(query + " " + _.as[String])
      ))
    )
    

    And not fuss with the "Nothing found!" message, but depending on what exactly you’re doing that might not be appropriate.

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

Sidebar

Related Questions

Haskell newbie here. I'm attempting to use the http-enumerator to query a service via
I have an enumeration that I want to use in pattern matches in an
One common pattern I see and use frequently in C++ is to temporarily set
Okay, this might be confusing. What I'm trying to do is use an enumerator
I want to use the enumerator for a generic collection with Delphi XE2. I
I'm trying to use the libxml-enumerator package on Windows, which (ultimately) needs to use
I'm trying to use the libxml-enumerator package on Windows, which (ultimately) needs to use
Is there some pedantic Enumerator-theory-based reason why the Any() method throws an ArgumentNullException for
Currently, there two popular choices which implement the iteratee pattern : The enumerator package
The Problem: I have a list with Objects as elements. I use enumerator to

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.