Skip to content

Commit

Permalink
Convenience methods from Try[T] => {Future, Promise}[T]
Browse files Browse the repository at this point in the history
  • Loading branch information
retronym committed Sep 19, 2013
1 parent 884e1ce commit 9fe6b69
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/library/scala/concurrent/Future.scala
Expand Up @@ -473,6 +473,13 @@ object Future {
*/
def successful[T](result: T): Future[T] = Promise.successful(result).future

/** Creates an already completed Future with the specified result or exception.
*
* @tparam T the type of the value in the promise
* @return the newly created `Future` object
*/
def fromTry[T](result: Try[T]): Future[T] = Promise.fromTry(result).future

/** Starts an asynchronous computation and returns a `Future` object with the result of that computation.
*
* The result becomes available once the asynchronous computation is completed.
Expand Down
11 changes: 9 additions & 2 deletions src/library/scala/concurrent/Promise.scala
Expand Up @@ -128,12 +128,19 @@ object Promise {
* @tparam T the type of the value in the promise
* @return the newly created `Promise` object
*/
def failed[T](exception: Throwable): Promise[T] = new impl.Promise.KeptPromise[T](Failure(exception))
def failed[T](exception: Throwable): Promise[T] = fromTry(Failure(exception))

/** Creates an already completed Promise with the specified result.
*
* @tparam T the type of the value in the promise
* @return the newly created `Promise` object
*/
def successful[T](result: T): Promise[T] = new impl.Promise.KeptPromise[T](Success(result))
def successful[T](result: T): Promise[T] = fromTry(Success(result))

/** Creates an already completed Promise with the specified result or exception.
*
* @tparam T the type of the value in the promise
* @return the newly created `Promise` object
*/
def fromTry[T](result: Try[T]): Promise[T] = new impl.Promise.KeptPromise[T](result)
}

0 comments on commit 9fe6b69

Please sign in to comment.