Scala Async alternatives and similar packages
Based on the "Extensions" category.
Alternatively, view Scala Async alternatives based on common mentions on social networks and blogs.
-
cats
Lightweight, modular, and extensible library for functional programming. -
Cassovary
Cassovary is a simple big graph processing library for the JVM -
Enumeratum
A type-safe, reflection-free, powerful enumeration implementation for Scala with exhaustive pattern match warnings and helpful integrations. -
scala.meta
Library to read, analyze, transform and generate Scala programs -
Scala-Logging
Convenient and performant logging library for Scala wrapping SLF4J. -
Chimney
Scala library for boilerplate-free, type-safe data transformations -
Freestyle
A cohesive & pragmatic framework of FP centric Scala libraries -
Scala Graph
Graph for Scala is intended to provide basic graph functionality seamlessly fitting into the Scala Collection Library. Like the well known members of scala.collection, Graph for Scala is an in-memory graph library aiming at editing and traversing graphs, finding cycles etc. in a user-friendly way. -
tinylog
tinylog is a lightweight logging framework for Java, Kotlin, Scala, and Android -
scribe
The fastest logging library in the world. Built from scratch in Scala and programmatically configurable. -
Each
A macro library that converts native imperative syntax to scalaz's monadic expressions -
Rapture
a collection of libraries for common, everyday programming tasks (I/O, JSON, i18n, etc.) -
Stateless Future
Asynchronous programming in fully featured Scala syntax. -
Scala Blitz
Scala framework for efficient sequential and data-parallel collections - -
Squid
Squid – type-safe metaprogramming and compilation framework for Scala -
Records for Scala
Labeled records for Scala based on structural refinement types and macros. -
Play monadic actions
A simple scala DSL to allow clean and monadic style for Play! Actions -
enableIf.scala
A library that toggles Scala code at compile-time, like #if in C/C++ -
Lamma
Lamma schedule generator for Scala is a professional schedule generation library for periodic schedules like fixed income coupon payment, equity deravitive fixing date generation etc. -
Freasy Monad
Easy way to create Free Monad using Scala macros with first-class Intellij support. -
wvlet-log
A library for enhancing your application logs with colors and source code locations. -
Freedsl
Practical effect composition library based on abstract wrapping type and the free monad -
Resolvable
A library to optimize fetching immutable data structures from several endpoints in several formats.
Build time-series-based applications quickly and at scale.
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Scala Async or a related project?
README
scala-async
A Scala DSL to enable a direct style of coding when composing Future
s.
Usage
As of scala-async 1.0, Scala 2.12.12+ or 2.13.3+ are required.
Add dependency
SBT Example
libraryDependencies += "org.scala-lang.modules" %% "scala-async" % "0.10.0"
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided
For Maven projects add the following to your (make sure to use the correct Scala version suffix to match your project’s Scala binary version):
Maven Example
<dependency>
<groupId>org.scala-lang.modules</groupId>
<artifactId>scala-async_2.13</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>2.13.8</version>
<scope>provided</scope>
</dependency>
Enable compiler support for async
Add the -Xasync
to the Scala compiler options.
SBT Example
scalacOptions += "-Xasync"
Maven Example
<project>
...
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.4.0</version>
<configuration>
<args>
<arg>-Xasync</arg>
</args>
</configuration>
</plugin>
...
</project>
Start coding
import scala.concurrent.ExecutionContext.Implicits.global
import scala.async.Async.{async, await}
val future = async {
val f1: Future[Boolean] = async { ...; true }
val f2 = async { ...; 42 }
if (await(f1)) await(f2) else 0
}
What is async
?
async
marks a block of asynchronous code. Such a block usually contains
one or more await
calls, which marks a point at which the computation
will be suspended until the awaited Future
is complete.
By default, async
blocks operate on scala.concurrent.{Future, Promise}
.
The system can be adapted to alternative implementations of the
Future
pattern.
Consider the following example:
def slowCalcFuture: Future[Int] = ... // 01
def combined: Future[Int] = async { // 02
await(slowCalcFuture) + await(slowCalcFuture) // 03
}
val x: Int = Await.result(combined, 10.seconds) // 05
Line 1 defines an asynchronous method: it returns a Future
.
Line 2 begins an async
block. During compilation,
the contents of this block will be analyzed to identify
the await
calls, and transformed into non-blocking
code.
Control flow will immediately pass to line 5, as the
computation in the async
block is not executed
on the caller's thread.
Line 3 begins by triggering slowCalcFuture
, and then
suspending until it has been calculated. Only after it
has finished, we trigger it again, and suspend again.
Finally, we add the results and complete combined
, which
in turn will release line 5 (unless it had already timed out).
It is important to note that while lines 1-4 are non-blocking, they are not parallel. If we wanted to parallelize the two computations, we could rearrange the code as follows:
def combined: Future[Int] = async {
val future1 = slowCalcFuture
val future2 = slowCalcFuture
await(future1) + await(future2)
}
Limitations
await
must be directly in the control flow of the async expression
The await
cannot be nested under a local method, object, class or lambda:
async {
List(1).foreach { x => await(f(x) } // invalid
}
await
must be not be nested within try
/ catch
/ finally
.
This implementation restriction may be lifted in future versions.
Comparison with direct use of Future
API
This computation could also be expressed by directly using the higher-order functions of Futures:
def slowCalcFuture: Future[Int] = ...
val future1 = slowCalcFuture
val future2 = slowCalcFuture
def combined: Future[Int] = for {
r1 <- future1
r2 <- future2
} yield r1 + r2
The async
approach has two advantages over the use of
map
and flatMap
:
- The code more directly reflects the programmer's intent,
and does not require us to name the results
r1
andr2
. This advantage is even more pronounced when we mix control structures inasync
blocks. async
blocks are compiled to a single anonymous class, as opposed to a separate anonymous class for each closure required at each generator (<-
) in the for-comprehension. This reduces the size of generated code, and can avoid boxing of intermediate results.