File tree Expand file tree Collapse file tree 2 files changed +44
-3
lines changed
src/main/scala/FuturesAndPromises Expand file tree Collapse file tree 2 files changed +44
-3
lines changed Original file line number Diff line number Diff line change 1- .idea
1+ .idea /
22
3- target
3+ target /
44
5- project
5+ project /
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments