Package org.apache.groovy.runtime.async

# Class AsyncClosureUtils

java.lang.Object
org.apache.groovy.runtime.async.AsyncClosureUtils

public final class AsyncClosureUtils extends Object
Closure-specific async utilities that have no pure-Java equivalent.

These methods create `Closure` subclasses and are therefore inherently Groovy-specific. They predate the `async` keyword and are retained for programmatic use. The `async { ... }` keyword syntax is preferred for most use cases.

Since:
  6.0.0
See Also:
  - `AsyncSupport`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static <T> Closure<Awaitable<T>>`
  `wrapAsync(Closure<T> closure)`
  Wraps a closure so that each invocation executes the body asynchronously and returns an `Awaitable`.
  `static <T> Closure<Iterable<T>>`
  `wrapAsyncGenerator(Closure<?> closure)`
  Wraps a generator closure so that each invocation returns an `Iterable` backed by a `GeneratorBridge`.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### wrapAsync

    public static <T> Closure<Awaitable<T>> wrapAsync(Closure<T> closure)
    Wraps a closure so that each invocation executes the body asynchronously and returns an `Awaitable`.

         def asyncTask = AsyncClosureUtils.wrapAsync { expensiveWork() }
         def result = await asyncTask()

    Type Parameters:
      `T` - the result type
    Parameters:
      `closure` - the closure to wrap
    Returns:
      a closure that returns an Awaitable on each call

  - ### wrapAsyncGenerator

    public static <T> Closure<Iterable<T>> wrapAsyncGenerator(Closure<?> closure)
    Wraps a generator closure so that each invocation returns an `Iterable` backed by a `GeneratorBridge`.
    Type Parameters:
      `T` - the element type
    Parameters:
      `closure` - the generator closure; receives a GeneratorBridge as its first parameter
    Returns:
      a closure that produces an Iterable on each call