AsyncCallback

final class AsyncCallback[+A] extends AnyVal

Pure asynchronous callback.

You can think of this as being similar to using Future - you can use it in for-comprehensions the same way - except AsyncCallback is pure and doesn't need an ExecutionContext.

When combining instances, it's good to know which methods are sequential and which are parallel (or at least concurrent).

The following methods are sequential:

The following methods are effectively parallel:

In order to actually run this, or get it into a shape in which in can be run, use one of the following:

A good example is the Ajax 2 demo.

Type Params
A

The type of data asynchronously produced on success.

Companion
object
class AnyVal
trait Matchable
class Any

Value members

Concrete methods

def *>[B](next: AsyncCallback[B]): AsyncCallback[B]

Start both this and the given callback at once and when both have completed successfully, discard the value produced by this.

Start both this and the given callback at once and when both have completed successfully, discard the value produced by this.

def <*[B](next: AsyncCallback[B]): AsyncCallback[A]

Start both this and the given callback at once and when both have completed successfully, discard the value produced by the given callback.

Start both this and the given callback at once and when both have completed successfully, discard the value produced by the given callback.

inline def <<[B](runBefore: AsyncCallback[B]): AsyncCallback[A]

Sequence a callback to run before this, discarding any value produced by it.

Sequence a callback to run before this, discarding any value produced by it.

def <<?[B](prev: Option[AsyncCallback[B]]): AsyncCallback[A]

Convenient version of << that accepts an Option

Convenient version of << that accepts an Option

inline def <|(t: A => Any): AsyncCallback[A]

Alias for tap.

Alias for tap.

def >>[B](runNext: AsyncCallback[B]): AsyncCallback[B]

Sequence the argument a callback to run after this, discarding any value produced by this.

Sequence the argument a callback to run after this, discarding any value produced by this.

inline def >>=[B](inline g: A => AsyncCallback[B]): AsyncCallback[B]

Alias for flatMap.

Alias for flatMap.

def attempt: AsyncCallback[Either[Throwable, A]]

Wraps this callback in a try-catch and returns either the result or the exception if one occurs.

Wraps this callback in a try-catch and returns either the result or the exception if one occurs.

def completeWith(f: Try[A] => Callback): Callback
inline def debounce(inline delay: Duration): AsyncCallback[A]
inline def debounce(inline delay: FiniteDuration): AsyncCallback[A]
def debounceMs(delayMs: Long): AsyncCallback[A]
def delay(dur: Duration): AsyncCallback[A]
def delay(dur: FiniteDuration): AsyncCallback[A]
def delayMs(milliseconds: Double): AsyncCallback[A]

Runs this async computation in the background.

Runs this async computation in the background.

Unlike fork_ this returns an AsyncCallback[Unit] instead of a Callback.

def distFn: A => AsyncCallback[B]
Extension method from AsyncCallback

Function distribution. See AsyncCallback.liftTraverse(f).id for the dual.

Function distribution. See AsyncCallback.liftTraverse(f).id for the dual.

def finallyRun[B](runFinally: AsyncCallback[B]): AsyncCallback[A]

Wraps this callback in a try-finally block and runs the given callback in the finally clause, after the current callback completes, be it in error or success.

Wraps this callback in a try-finally block and runs the given callback in the finally clause, after the current callback completes, be it in error or success.

inline def finallyRunSync[B](runFinally: CallbackTo[B]): AsyncCallback[A]

Wraps this callback in a try-finally block and runs the given callback in the finally clause, after the current callback completes, be it in error or success.

Wraps this callback in a try-finally block and runs the given callback in the finally clause, after the current callback completes, be it in error or success.

def flatMap[B](f: A => AsyncCallback[B]): AsyncCallback[B]
def flatMapSync[B](f: A => CallbackTo[B]): AsyncCallback[B]
def flatTap[B](t: A => AsyncCallback[B]): AsyncCallback[A]
def flatTapSync[B](t: A => CallbackTo[B]): AsyncCallback[A]
def flatten[B](using ev: A => AsyncCallback[B]): AsyncCallback[B]
def flattenSync[B](using ev: A => CallbackTo[B]): AsyncCallback[B]

Runs this async computation in the background.

Runs this async computation in the background.

Returns the ability for you to await/join the forked computation.

inline def fork_: Callback

Runs this async computation in the background.

Runs this async computation in the background.

Unlike fork this returns nothing, meaning this is like fire-and-forget.

def handleError[AA >: A](f: Throwable => AsyncCallback[AA]): AsyncCallback[AA]
def handleErrorSync[AA >: A](f: Throwable => CallbackTo[AA]): AsyncCallback[AA]
def logAround(message: Any, optionalParams: Any*): AsyncCallback[A]

Log to the console before this callback starts, and after it completes.

Log to the console before this callback starts, and after it completes.

Does not change the result.

def logDuration(fmt: FiniteDuration => String): AsyncCallback[A]

Log the duration of this callback's execution.

Log the duration of this callback's execution.

def logDuration(name: String): AsyncCallback[A]

Log the duration of this callback's execution.

Log the duration of this callback's execution.

Value Params
name

Prefix to appear the log output.

Log the duration of this callback's execution.

Log the duration of this callback's execution.

def logResult(msg: A => String): AsyncCallback[A]

Logs the result of this callback as it completes.

Logs the result of this callback as it completes.

def logResult(name: String): AsyncCallback[A]

Logs the result of this callback as it completes.

Logs the result of this callback as it completes.

Value Params
name

Prefix to appear the log output.

Logs the result of this callback as it completes.

Logs the result of this callback as it completes.

def map[B](f: A => B): AsyncCallback[B]
def maybeHandleError[AA >: A](f: PartialFunction[Throwable, AsyncCallback[AA]]): AsyncCallback[AA]
def maybeHandleErrorSync[AA >: A](f: PartialFunction[Throwable, CallbackTo[AA]]): AsyncCallback[AA]
def memo(): AsyncCallback[A]

Return a version of this callback that will only execute once, and reuse the result for all other invocations.

Return a version of this callback that will only execute once, and reuse the result for all other invocations.

def race[B](that: AsyncCallback[B]): AsyncCallback[Either[A, B]]

Start both this and the given callback at once use the first result to become available, regardless of whether it's a success or failure.

Start both this and the given callback at once use the first result to become available, regardless of whether it's a success or failure.

inline def rateLimit(inline window: Duration): AsyncCallback[Option[A]]

Limits the number of invocations in a given amount of time.

Limits the number of invocations in a given amount of time.

Returns

Some if invocation was allowed, None if rejected/rate-limited

inline def rateLimit(inline window: FiniteDuration): AsyncCallback[Option[A]]

Limits the number of invocations in a given amount of time.

Limits the number of invocations in a given amount of time.

Returns

Some if invocation was allowed, None if rejected/rate-limited

inline def rateLimit(inline window: Duration, inline maxPerWindow: Int): AsyncCallback[Option[A]]

Limits the number of invocations in a given amount of time.

Limits the number of invocations in a given amount of time.

Returns

Some if invocation was allowed, None if rejected/rate-limited

inline def rateLimit(inline window: FiniteDuration, inline maxPerWindow: Int): AsyncCallback[Option[A]]

Limits the number of invocations in a given amount of time.

Limits the number of invocations in a given amount of time.

Returns

Some if invocation was allowed, None if rejected/rate-limited

def rateLimitMs(windowMs: Long, maxPerWindow: Int): AsyncCallback[Option[A]]

Limits the number of invocations in a given amount of time.

Limits the number of invocations in a given amount of time.

Returns

Some if invocation was allowed, None if rejected/rate-limited

def reset: AsyncCallback[Unit]

If this completes successfully, discard the result. If any exception occurs, call printStackTrace and continue.

If this completes successfully, discard the result. If any exception occurs, call printStackTrace and continue.

Since

2.0.0

def ret[B](b: B): AsyncCallback[B]

Discard the callback's return value, return a given value instead.

Discard the callback's return value, return a given value instead.

ret, short for return.

def runNow(): Unit
inline def setInterval(inline dur: Duration): CallbackTo[SetIntervalResult]

Schedule for repeated execution every dur.

Schedule for repeated execution every dur.

inline def setInterval(inline dur: FiniteDuration): CallbackTo[SetIntervalResult]

Schedule for repeated execution every dur.

Schedule for repeated execution every dur.

inline def setIntervalMs(inline milliseconds: Double): CallbackTo[SetIntervalResult]

Schedule for repeated execution every x milliseconds.

Schedule for repeated execution every x milliseconds.

inline def setTimeout(inline dur: Duration): CallbackTo[SetTimeoutResult]

Schedule for execution after dur.

Schedule for execution after dur.

Note: it most cases delay is a better alternative.

inline def setTimeout(inline dur: FiniteDuration): CallbackTo[SetTimeoutResult]

Schedule for execution after dur.

Schedule for execution after dur.

Note: it most cases delay is a better alternative.

inline def setTimeoutMs(inline milliseconds: Double): CallbackTo[SetTimeoutResult]

Schedule for execution after x milliseconds.

Schedule for execution after x milliseconds.

Note: it most cases delayMs is a better alternative.

def sync: CallbackTo[Either[AsyncCallback[A], A]]

Returns a synchronous Callback that when run, returns the result on the Right if this AsyncCallback is actually synchronous, else returns a new AsyncCallback on the Left that waits for the async computation to complete.

Returns a synchronous Callback that when run, returns the result on the Right if this AsyncCallback is actually synchronous, else returns a new AsyncCallback on the Left that waits for the async computation to complete.

def tap(t: A => Any): AsyncCallback[A]

When the callback result becomes available, perform a given side-effect with it.

When the callback result becomes available, perform a given side-effect with it.

def timeout(limit: Duration): AsyncCallback[Option[A]]

Limit the amount of time you're prepared to wait for a computation.

Limit the amount of time you're prepared to wait for a computation.

Note: there's no built-in concept of cancellation here. If your procedure doesn't finish in time, this will return None when the time limit is reached however, the underlying procedure will become orphaned and continue to run in the background until complete.

def timeout(limit: FiniteDuration): AsyncCallback[Option[A]]

Limit the amount of time you're prepared to wait for a computation.

Limit the amount of time you're prepared to wait for a computation.

Note: there's no built-in concept of cancellation here. If your procedure doesn't finish in time, this will return None when the time limit is reached however, the underlying procedure will become orphaned and continue to run in the background until complete.

def timeoutMs(milliseconds: Double): AsyncCallback[Option[A]]

Limit the amount of time you're prepared to wait for a computation.

Limit the amount of time you're prepared to wait for a computation.

Note: there's no built-in concept of cancellation here. If your procedure doesn't finish in time, this will return None when the time limit is reached however, the underlying procedure will become orphaned and continue to run in the background until complete.

inline def unless(inline cond: Boolean): AsyncCallback[Option[A]]

Conditional execution of this callback. Reverse of when.

Conditional execution of this callback. Reverse of when.

Value Params
cond

The condition required to be false for this callback to execute.

Returns

Some result of the callback executed, else None.

inline def unless_(inline cond: Boolean): AsyncCallback[Unit]

Conditional execution of this callback. Discards the result. Reverse of when_.

Conditional execution of this callback. Discards the result. Reverse of when_.

Value Params
cond

The condition required to be false for the callback to execute.

THIS IS VERY CONVENIENT IN UNIT TESTS BUT DO NOT RUN THIS IN PRODUCTION CODE.

THIS IS VERY CONVENIENT IN UNIT TESTS BUT DO NOT RUN THIS IN PRODUCTION CODE.

Executes this now, expecting and returning a synchronous result. If there are any asynchronous computations this will throw an exception.

def unsafeToFuture(): Future[A]
def unsafeToJsPromise(): Promise[A]
def void: AsyncCallback[Unit]

Discard the value produced by this callback.

Discard the value produced by this callback.

inline def voidExplicit[B](using inline ev: A <:< B): AsyncCallback[Unit]

Discard the value produced by this callback.

Discard the value produced by this callback.

This method allows you to be explicit about the type you're discarding (which may change in future).

def when(cond: => Boolean): AsyncCallback[Option[A]]

Conditional execution of this callback.

Conditional execution of this callback.

Value Params
cond

The condition required to be true for this callback to execute.

Returns

Some result of the callback executed, else None.

inline def when_(inline cond: Boolean): AsyncCallback[Unit]

Conditional execution of this callback. Discards the result.

Conditional execution of this callback. Discards the result.

Value Params
cond

The condition required to be true for this callback to execute.

def withDuration[B](f: (A, FiniteDuration) => AsyncCallback[B]): AsyncCallback[B]

Record the duration of this callback's execution.

Record the duration of this callback's execution.

def withFilter(f: A => Boolean): AsyncCallback[A]
def zip[B](that: AsyncCallback[B]): AsyncCallback[(A, B)]
def zipWith[B, C](that: AsyncCallback[B])(f: (A, B) => C): AsyncCallback[C]
inline def |>[B](f: A => B): AsyncCallback[B]

Alias for map.

Alias for map.

Concrete fields

val underlyingRepr: () => A