Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwix committed Feb 2, 2021
0 parents commit eecc483
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
*.class
*.log

# sbt specific
.cache/
.history/
.lib/
dist/*
target/
lib_managed/
src_managed/
project/boot/
project/plugins/project/

# Scala-IDE specific
.scala_dependencies
.worksheet

# IntelliJ
.idea/
*.iml
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Common Use Cases of Scala Futures

## Overview

This document contains common use cases of using Scala Futures.

## Introduction

Futures provide a simple way to run an asynchronous computation. Future starts a computation when you create it and then eventually returns the result. For example, every RPC invocation at Wix is a function that returns a Future of the RPC service result.

Futures are composable. We consider the following common cases of Futures composition:
Sequential composition with or without error accumulation;
Concurrent composition with or without error accumulation;

## Sequential Processing without Error Accumulation
Suppose there are functions fad, fbc, and fcd:
```
def fab(a: A): Future[B] = ???
def fbc(b: B): Future[C] = ???
def fcd(c: C): Future[D] = ???
```

We are writing a new function foo: A => Future[D] to invoke fad, fbc, and fcd sequentially. This function foo fails fast, i.e. returns the first failed future returned by one of fad, fbc, and fcd

## Sequential Processing with Error Accumulation

Suppose there are functions fad, fbd, and fcd:
```
def fad(a: A): Future[D] = ???
def fbd(b: B): Future[D] = ???
def fcd(c: C): Future[D] = ???
```

We are writing a new function foo: (A, B, C) => Future[D] to invoke fad, and then if it fails invoke fbd, and then if it fails invoke fcd. This function foo does not fail fast and returns a failure only if fad, fbd, and fcd fail.

Example: invocation with fallbacks.

## Concurrent Processing without Error Accumulation

Suppose there are functions fad, fbc, fcd and case class BCD(b: B, c: C, d: D)
```
def fab(a: A): Future[B] = ???
def fac(a: A): Future[C] = ???
def fad(a: A): Future[D] = ???
```

We are writing a new function foo: A => Future[BCD] to invoke fad, fbc, and fcd concurrently. The composite function foo fails fast, i.e. foo returns the first failure returned by one of fab, fac, and fad

## Concurrent Processing with Errors Accumulation


83 changes: 83 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.wix</groupId>
<artifactId>async-workshop</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Async Workshop</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.binary.version>2.12</scala.binary.version>
<scala.version>2.12.4</scala.version>
<specs2.version>3.8.9</specs2.version>
</properties>

<dependencies>

<!-- scala library -->

<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>

<!-- specs2 for tests -->

<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-core_${scala.binary.version}</artifactId>
<version>${specs2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-junit_${scala.binary.version}</artifactId>
<version>${specs2.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<args>
<arg>-unchecked</arg>
<arg>-deprecation</arg>
</args>
<addJavacArgs>-deprecation</addJavacArgs>
<source>1.8</source>
<target>1.8</target>
<scalaCompatVersion>${scala.binary.version}</scalaCompatVersion>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

0 comments on commit eecc483

Please sign in to comment.