0

I would like to get the http status code of a request made with scalajs-angular.

I can get it with http.get[js.Any]("url") success and a function of type (js.Any, Int) => Unit. But I would like to return something else than Unit.

For instance, I would like to be able to do:

http.get[js.Any]("url") map {
    case response.status == 200 => true
    case _ => false 
}

How can I do this?

Simon
  • 6,025
  • 7
  • 46
  • 98

1 Answers1

0

Fleshing out what I said in the comment below (now that I'm no longer on a phone): based on the docs of scalajs-angular, it looks like its version of get returns a version of Promise. What you're looking for is apparently a Future. So I suspect your code will work as intended if you add .future, which gets a Future from a Promise:

http.get[js.Any]("url").future map {
  case response.status == 200 => true
  case _ => false 
}

(NB: I don't use scalajs-angular myself, so this is a guess based on the documentation.)

Justin du Coeur
  • 2,699
  • 1
  • 14
  • 19
  • I'm aware that get is asynchronous, but I don't understand why I can't get the status of the response since I can get the data of this response. Moreover I can get it when it goes in the recover part: (`recover { case t: HttpException => t.status}`) – Simon Apr 14 '16 at 14:44
  • And can you confirm me that you're saying that it is not possible to get the http status returned by a http request as a result (wrapped in a Future) of a function. – Simon Apr 14 '16 at 15:03
  • Oh, I see - my apologies, I misunderstood your question. I haven't used scalajs-angular myself, but from the docs it appears that is version of get returns an HttpPromise, which extends Promise. Which I believe means you should be able to call .future on that to get a Future, and you should be able to map over that... – Justin du Coeur Apr 15 '16 at 19:13