Skip to content

Commit d3c496d

Browse files
committed
Finished blogpost on Future Trys
1 parent d7e5d30 commit d3c496d

7 files changed

+63
-44
lines changed

_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Site settings
22
title: Stormlantern Blog
3-
email: dirk,[email protected]
3+
email: dirk.[email protected]
44
description: >
55
Blogging home for Dirk Louwers
66
baseurl: "" # the subpath of your site, e.g. /blog/

_drafts/usage-of-future-try.markdown

Lines changed: 0 additions & 2 deletions
This file was deleted.

_posts/2014-07-15-musings-on-setting-up-nice-web.html

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
layout: post
3+
title: Musings on setting up a nice web application structure using Spray and ScalaJS
4+
date: '2014-07-15T14:33:00.002-07:00'
5+
author: Dirk Louwers
6+
tags:
7+
- spray
8+
- sbt
9+
- scalajs
10+
- scala
11+
modified_time: '2014-07-16T02:17:02.238-07:00'
12+
blogger_id: tag:blogger.com,1999:blog-1529483519632285628.post-2473681211825466512
13+
blogger_orig_url: http://dogtyped.blogspot.com/2014/07/musings-on-setting-up-nice-web.html
14+
---
15+
Today I have been experimenting with setting up a nice project structure for web
16+
applications using Spray and ScalaJS. First I considered the simplest option of
17+
combining them in a single SBT project but this seems to be a bad idea. ScalaJS
18+
generates class files and a jar. Mixing this with Scala backend code would
19+
result in a bloated jar full of unused code.
20+
21+
So the next step is using two sub projects inside of SBT. One for the frontend
22+
and one for the backend. However, in itself this is not enough. I would like my
23+
html to reside in the frontend resources. The Javascript will be generated in
24+
this project by fullOptJS and fastOptJS and end up in the project's target
25+
directory. I'd like the generated Javascript and mapping files to be available
26+
in resources. The backend project should have these resources available but NOT
27+
the classes.
28+
29+
Next up I will be trying to see if this can be accomplished or if there is a
30+
better way to go about this.
Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
---
22
layout: post
3-
title: Peace of mind in a state of overload
3+
title: Try is free in the Future
44
date: '2015-01-18T18:21:00.000+02:00'
55
author: Dirk Louwers
66
tags:
77
- scala
88
modified_time: '2015-01-18T18:21:00.000+02:00'
99
---
10-
# Try is free in the Future
1110
Lately I have seen a few developers consistently use a Try inside of a Future in
1211
order to make error handling easier. Here I will investigate if this has any
1312
merits or whether a Future on it's own offers enough error handle.
1413

15-
If you look at the following code there is nothing that a `Future` can't supply
16-
but a `Try` can.
14+
If you look at the following code there is nothing that a Future can't supply
15+
but a Try can:
1716

1817
{% highlight scala %}
1918
import scala.concurrent.ExecutionContext.Implicits.global
20-
import scala.concurrent.{Await, Future}
19+
import scala.concurrent.{Await, Future, Awaitable}
2120
import scala.concurrent.duration._
21+
import scala.util.{Try, Success, Failure}
2222

2323
object Main extends App {
2424

@@ -33,36 +33,43 @@ object Main extends App {
3333
}
3434

3535
// We would want to wrap the result into a hypothetical http response
36-
case class Response(code: Int, body: Array[Byte])
37-
38-
object Response {
39-
def fromInt(int: Int): Response = {
40-
val buffer = java.nio.ByteBuffer.allocate(4)
41-
buffer.putInt(int)
42-
Response(200, buffer.array())
43-
}
44-
}
36+
case class Response(code: Int, body: String)
4537

4638
// This is the handler we will use
4739
def handle[T](future: Future[T]): Future[Response] = {
4840
future.map {
49-
case answer: Int => Response.fromInt(answer)
41+
case answer: Int => Response(200, answer.toString)
5042
} recover {
51-
case t: Throwable => Response(500, Array.empty[Byte])
43+
case t: Throwable => Response(500, "Uh oh!")
5244
}
5345
}
5446

5547
{
5648
val result = Await.result(handle(happyFuture), 1 second)
57-
assert(result.code == 200)
58-
val buffer = java.nio.ByteBuffer.wrap(result.body)
59-
assert(buffer.getInt == 42)
49+
println(result)
6050
}
6151

6252
{
6353
val result = Await.result(handle(bleakFuture), 1 second)
64-
assert(result.code == 500)
54+
println(result)
6555
}
6656
}
57+
{% endhighlight %}
58+
59+
After giving it some thought the only situation where I could imagine Try
60+
being useful in conjunction with Future is when awaiting a Future but not wanting
61+
to deal with error situations yet. The times I would be awaiting a future are
62+
very few in practice though. But when needed something like this migth do:
6763

64+
{% highlight scala %}
65+
object TryAwait {
66+
def ready[T](awaitable: Awaitable[T], atMost: Duration): Try[T] = {
67+
Try {
68+
Await.result(awaitable, atMost)
69+
}
70+
}
71+
}
6872
{% endhighlight %}
73+
74+
If you do feel that using Trys inside of Futures adds value to your codebase
75+
please let me know.

about.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ title: About
44
permalink: /about/
55
---
66

7-
This is the base Jekyll theme. You can find out more info about customizing your Jekyll theme, as well as basic Jekyll usage documentation at [jekyllrb.com](http://jekyllrb.com/)
8-
9-
You can find the source code for the Jekyll new theme at: [github.com/jglovier/jekyll-new](https://github.com/jglovier/jekyll-new)
10-
11-
You can find the source code for Jekyll at [github.com/jekyll/jekyll](https://github.com/jekyll/jekyll)
7+
This is my blog. My name is Dirk Louwers. I will write about anything that
8+
piques my interest.

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ <h1 class="page-heading">Posts</h1>
1414
<h2>
1515
<a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
1616
</h2>
17+
<p>
18+
{{ post.excerpt }}
19+
</p>
1720
</li>
1821
{% endfor %}
1922
</ul>

0 commit comments

Comments
 (0)