Skip to content

Commit

Permalink
add mapVal
Browse files Browse the repository at this point in the history
  • Loading branch information
t3hnar committed Jul 21, 2018
1 parent e7dba03 commit 1984572
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ object FutureHelper {


implicit class FutureOps[T](val self: Future[T]) extends AnyVal {
def unit: Future[Unit] = self.map { _ => () }(CurrentThreadExecutionContext)
def mapVal[TT](value: TT): Future[TT] = self.map(_ => value)(CurrentThreadExecutionContext)
def unit: Future[Unit] = mapVal(())
def flatten[S](implicit ev: T <:< Future[S]): Future[S] = self.flatMap(ev)(CurrentThreadExecutionContext)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class FutureHelperSpec extends FunSuite with Matchers {

promise.success(2)

Await.result(future, 3.seconds) shouldEqual List(1, 2,3)
Await.result(future, 3.seconds) shouldEqual List(1, 2, 3)
}

test("mapTo") {
val future = Future.successful(1).mapVal("1")
Await.result(future, 3.seconds) shouldEqual "1"
}
}

0 comments on commit 1984572

Please sign in to comment.