Skip to content

Commit a499322

Browse files
aded new examples
1 parent 781c914 commit a499322

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
.idea
1+
.idea/
22

3-
target
3+
target/
44

5-
project
5+
project/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package FuturesAndPromises
2+
3+
import scala.concurrent.ExecutionContext.Implicits.global
4+
import scala.concurrent.Future
5+
import scala.util.{Failure, Success}
6+
7+
/**
8+
* A reminder that firstCompletedOf() is non-deterministic and,
9+
* it will return any one of the futures which finish first
10+
* */
11+
object ExampleFirstCompletedOf {
12+
def checkIsBig(input: Int): Future[Option[Int]] = {
13+
Future(
14+
if (input > 0) {
15+
Some(20)
16+
} else {
17+
None
18+
}
19+
)
20+
}
21+
22+
def main(args: Array[String]): Unit = {
23+
val futures = List(
24+
checkIsBig(1),
25+
checkIsBig(-1),
26+
checkIsBig(8),
27+
checkIsBig(9),
28+
checkIsBig(11),
29+
checkIsBig(7)
30+
)
31+
32+
// Call Future.firstCompletedOf to get the results of the first future that completes
33+
val futureFirstCompletedResult = Future.firstCompletedOf(futures)
34+
futureFirstCompletedResult.onComplete {
35+
case Success(results) => println(s"Results $results")
36+
case Failure(e) => println(s"Error processing future operations, error = ${e.getMessage}")
37+
}
38+
39+
Thread.sleep(100)
40+
}
41+
}

0 commit comments

Comments
 (0)