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 8604127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:29:36+00:00 2026-06-12T02:29:36+00:00

I’m learning scala, play, and web services all at once so bear with me.

  • 0

I’m learning scala, play, and web services all at once so bear with me. I’ve set up a little aggregator service that combines a weather web service and google’s geocode and places web services. I’ve got something working but I’m a little confused on the proper way to handle errors. (I posted the code at the end of the post)

So the places api uses lat/long and so I use the geocode api to get lat/long from the zip code. When processing the response from the call to the geocode api I end up with an (Option[String], Option[String]) (held in the maybeLoc val). Inside the match statement that checks maybeLoc, if it ends up being (None, None), I return Promise() because I need to return a Promise from the flatmap call.

I have two questions about this:

1.) What is the right way to handle the case of not being able to do any further processing while inside one of these flatMap or map calls? It requires me to return a promise, but making an empty Promise that will just time out when I go to redeem it seems like a really bad idea.

2.) Am I right in assuming that the call to Promise() makes an empty promise object that will always time out when trying to redeem it? I couldn’t really tell from the scaladoc and couldn’t find anything about it from google.

I hope my questions make sense to you and are clear enough. Here is the code:

def bothAsJson(zipcode:String) = Action {
    val promiseOfLoc = Geocode.buildUrlFor(zipcode).get()
    val promiseOfWeather = Weather.buildUrlFor(zipcode, "json").get()

    val result = promiseOfLoc.flatMap { locResp => 
        val maybeLoc = Geocode.extractLocation(locResp.body.toString())
        maybeLoc match {
            case (Some(lat), Some(lng)) => {
                val promiseOfPlaces = Places.buildUrlFor(lat,lng).get()
                promiseOfPlaces.flatMap { placesResp =>
                    promiseOfWeather.map { weatherResp =>
                        (weatherResp.body.toString(), placesResp.body.toString())
                    }
                }
            }
            case _ => Promise()
        }
    }

    Async {
        result.orTimeout("Timeout!", 2000).map {response =>
            response.fold(
                result => Ok("Got:\n\nweather:\n" + result._1 + "\n\nplaces:\n" + result._2),
                timeout => InternalServerError(timeout)
            )
        }
    }
}
  • 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-12T02:29:37+00:00Added an answer on June 12, 2026 at 2:29 am

    If you get (None, None) you shouldn’t be looking to timeout, but return another error message I believe. I’ve provided an example below.

    I think you need OptionT from from scalaz 7. I would write this as:

    import scalaz._
    import Scalaz._
    
    def bothAsJson(zipcode:String) = Action {
        val promiseOfLoc = Geocode.buildUrlFor(zipcode).get.map { Option(_.body.toString()) }
        val promiseOfWeather = Weather.buildUrlFor(zipcode, "json").get
           .map{ lockResp => 
               val (lat,lng) = Geocode.extractLocation(locResp.body.toString())
               (lat |@| lng).tupled
           }
        def buildPlaces(lat: String, lng: String) = Places.buildUrlFor(lat,lng).get
           .map { Option(_.body.toString) }
    
        val result = (for {
           (lat, lng) <- OptionT(promiseOfLoc)
           places     <- OptionT(Places.buildUrlFor(lat,lng).get())
           weather    <- OptionT(promiseOfWeather)
        } yield (places, weather)).run
    
        Async {
            result.orTimeout("Timeout!", 2000).map {response =>
                response.fold(
                    result => {
                      result.map(
                       some => Ok("Got:\n\nweather:\n" + some._1 + "\n\nplaces:\n" + some._2)
                      ).getOrElse(BadRequest("lat/lng failed probably?"))
                    },
                    timeout => InternalServerError(timeout)
                )
            }
        }
    }
    

    With OptionT we can flatMap as if it was an Option, gaining the ability not to process anything if we get a None. At the end we are left with a Promise[Option[T]] which is very nice for this. Another way good way to handle errors is to use Either/EtherT with the same method.

    |@| is an Applicative Builder. It takes 2 Options, and returning an Option((Int, Int)) if both sides are Some. If one side or both sides are None, it returns a None.

    Note for this to work you will need a scalaz Monad[Promise] instance

    implicit val PromiseInstance = new Monad[Promise] {
      // override def map[A,B](fa: Promise[A])(f: A => B) = fa.map(f)
      def point[A](a: => A) = Promise.pure(a)
      def bind[A,B](fa: Promise[A])(f: A => Promise[B]) = fa.flatMap(f)
    }
    

    Also please note that I have written all this code in the SO editor, There could be braces missing. But all the code should be more or less right, I tested parts of it in the repl.

    Please feel free to ask for help on #scalaz on freenode irc, or on the scalaz google groups.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this

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.