final class Task[+A] extends AnyRef
Trampolined computation producing an A
that may
include asynchronous steps. Arbitrary monadic expressions involving
map
and flatMap
are guaranteed to use constant stack space.
In addition, using Task.async
, one may construct a Task
from
callback-based APIs. This makes Task
useful as a concurrency primitive
and as a control structure for wrapping callback-based APIs with a more
straightforward, monadic API.
Task is also exception-safe. Any exceptions raised during processing may
be accessed via the attempt
method, which converts a Task[A]
to a
Task[Attempt[A]]
.
Unlike scala.concurrent.Future
, map
and flatMap
do NOT spawn new
tasks and do not require an implicit ExecutionContext
. Instead, map
and flatMap
merely add to the current (trampolined) continuation that
will be run by the 'current' thread, unless explicitly forked via Task.start
.
Task
also differs from scala.concurrent.Future
in that it
does not represent a _running_ computation. Instead, we
reintroduce concurrency _explicitly_ using the Task.start
function.
This simplifies our implementation and makes code easier to reason about,
since the order of effects and the points of allowed concurrency are made
fully explicit and do not depend on Scala's evaluation order.
- Source
- Task.scala
- Alphabetic
- By Inheritance
- Task
- AnyRef
- Any
- by JvmSyntax
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
- def +(other: String): String
- def ->[B](y: B): (Task[A], B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
async(implicit S: Strategy): Task[A]
Returns a task that performs all evaluation asynchronously.
-
def
attempt: Task[Attempt[A]]
'Catches' exceptions in the given task and returns them as values.
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
ensure(failure: ⇒ Throwable)(f: (A) ⇒ Boolean): Task[A]
Ensures the result of this Task satisfies the given predicate, or fails with the given value.
- def ensuring(cond: (Task[A]) ⇒ Boolean, msg: ⇒ Any): Task[A]
- def ensuring(cond: (Task[A]) ⇒ Boolean): Task[A]
- def ensuring(cond: Boolean, msg: ⇒ Any): Task[A]
- def ensuring(cond: Boolean): Task[A]
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
- def flatMap[B](f: (A) ⇒ Task[B]): Task[B]
- def formatted(fmtstr: String): String
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
-
def
handle[B >: A](f: PartialFunction[Throwable, B]): Task[B]
Calls
attempt
and handles some exceptions using the given partial function, calling Task.now on the result.Calls
attempt
and handles some exceptions using the given partial function, calling Task.now on the result. Any nonmatching exceptions are reraised. -
def
handleWith[B >: A](f: PartialFunction[Throwable, Task[B]]): Task[B]
Calls
attempt
and handles some exceptions using the given partial function.Calls
attempt
and handles some exceptions using the given partial function. Any nonmatching exceptions are reraised. -
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def map[B](f: (A) ⇒ B): Task[B]
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
-
def
or[B >: A](t2: Task[B]): Task[B]
Runs this
Task
, and if it fails with an exception, runst2
.Runs this
Task
, and if it fails with an exception, runst2
. This is rather coarse-grained. Useattempt
,handle
, andflatMap
for more fine grained control of exception handling. -
def
race[B](t: Task[B])(implicit S: Strategy): Task[Either[A, B]]
Returns a
Task
that, when run, races evaluation ofthis
andt
, and returns the result of whichever completes first.Returns a
Task
that, when run, races evaluation ofthis
andt
, and returns the result of whichever completes first. The losing task continues to execute in the background though its result will be sent nowhere. -
def
schedule(delay: FiniteDuration)(implicit strategy: Strategy, scheduler: Scheduler): Task[A]
Create a
Task
that will evaluatea
after at least the given delay. -
val
self: Task[A]
- Implicit
- This member is added by an implicit conversion from Task[A] to Task.JvmSyntax[A] performed by method JvmSyntax in fs2.TaskPlatform.
- Definition Classes
- JvmSyntax
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- Task → AnyRef → Any
-
def
unsafeAttemptRun(): Either[Throwable, A]
Like
unsafeRun
, but returns exceptions as values.Like
unsafeRun
, but returns exceptions as values.- Implicit
- This member is added by an implicit conversion from Task[A] to Task.JvmSyntax[A] performed by method JvmSyntax in fs2.TaskPlatform.
- Definition Classes
- JvmSyntax
-
def
unsafeAttemptRunFor(timeout: FiniteDuration): Either[Throwable, A]
Like
unsafeRunFor
, but returns exceptions as values.Like
unsafeRunFor
, but returns exceptions as values. BothTimeoutException
and other exceptions will be folded into the sameThrowable
.- Implicit
- This member is added by an implicit conversion from Task[A] to Task.JvmSyntax[A] performed by method JvmSyntax in fs2.TaskPlatform.
- Definition Classes
- JvmSyntax
-
def
unsafeAttemptRunSync(): Either[(Callback[A]) ⇒ Unit, Attempt[A]]
Like
unsafeRunSync
, but returns exceptions as values. -
def
unsafeAttemptValue(): Option[Attempt[A]]
Like
unsafeValue
, but returns exceptions as values. -
def
unsafeRun(): A
Run this
Task
and block until its result is available.Run this
Task
and block until its result is available. This will throw any exceptions generated by theTask
. To return exceptions in anEither
, useunsafeAttemptRun()
.- Implicit
- This member is added by an implicit conversion from Task[A] to Task.JvmSyntax[A] performed by method JvmSyntax in fs2.TaskPlatform.
- Definition Classes
- JvmSyntax
-
def
unsafeRunAsync(f: (Attempt[A]) ⇒ Unit): Unit
Run this computation to obtain either a result or an exception, then invoke the given callback.
Run this computation to obtain either a result or an exception, then invoke the given callback. Any pure, non-asynchronous computation at the head of this
Future
will be forced in the calling thread. At the firstAsync
encountered, control is transferred to whatever thread backs theAsync
and this function returns immediately. -
def
unsafeRunAsyncFuture(): Future[A]
Run this computation and return the result as a standard library
Future
.Run this computation and return the result as a standard library
Future
. LikeunsafeRunAsync
but returns a standard libraryFuture
instead of requiring a callback. -
def
unsafeRunFor(timeout: FiniteDuration): A
Run this
Task
and block until its result is available, or untiltimeout
has elapsed, at which point aTimeoutException
will be thrown and theFuture
will attempt to be canceled.Run this
Task
and block until its result is available, or untiltimeout
has elapsed, at which point aTimeoutException
will be thrown and theFuture
will attempt to be canceled.- Implicit
- This member is added by an implicit conversion from Task[A] to Task.JvmSyntax[A] performed by method JvmSyntax in fs2.TaskPlatform.
- Definition Classes
- JvmSyntax
-
def
unsafeRunSync(): Either[(Callback[A]) ⇒ Unit, A]
Runs this
Task
up until an async boundary.Runs this
Task
up until an async boundary. If the task completes synchronously, the result is returned wrapped in aRight
. If an async boundary is encountered, a continuation is returned wrapped in aLeft
. To return exceptions in anEither
, useunsafeAttemptRunSync()
. -
def
unsafeTimed(timeout: FiniteDuration)(implicit S: Strategy, scheduler: Scheduler): Task[A]
A
Task
which returns aTimeoutException
aftertimeout
, and attempts to cancel the running computation.A
Task
which returns aTimeoutException
aftertimeout
, and attempts to cancel the running computation.This method is unsafe because upon reaching the specified timeout, the running task is interrupted at a non-determinstic point in its execution.
-
def
unsafeValue(): Option[A]
Runs this
Task
up until an async boundary.Runs this
Task
up until an async boundary. If the task completes synchronously, the result is returned. If an async boundary is encountered,None
is returned. To return exceptions in anEither
, useunsafeAttemptValue()
. -
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
- def →[B](y: B): (Task[A], B)