Search code examples
project-reactorcallablemutiny

mutiny - what is the counterpart to Project Reactor's Mono.fromCallable()


I am new to Mutiny but have familiarity with Project Reactor.

I am looking for the counterpart to Mono.fromCallable() in Mutiny to create a Uni from a Callable or anonymous lambda?


Solution

  • You can create a Uni from a lambda using:

    Uni.createFrom().item(() -> "Testing Uni");
    

    For java.util.concurrent.Callable:

    Uni.createFrom().future( new FutureTask<>( callable ));
    

    Other ways you can create a Uni:

    Uni.createFrom().item(1); // Emit 1 at subscription time
    Uni.createFrom().item(() -> x); // Emit x at subscription time, the supplier is invoked for each subscription
    Uni.createFrom().completionStage(cs); // Emit the item from this completion stage
    Uni.createFrom().completionStage(() -> cs); // Emit the item from this completion stage, the stage is not created before subscription
    Uni.createFrom().failure(exception); // Emit the failure at subscription time
    Uni.createFrom().deferred(() -> Uni.from().value(x)); // Defer the uni creation until subscription. Each subscription can produce a different uni
    Uni.createFrom().nullItem(); // Emit null at subscription time
    Uni.createFrom().voidItem(); // Create a Uni not emitting any signal (returns Uni<Void>()
    Uni.createFrom().nothing(); // Create a Uni not emitting any signal
    Uni.createFrom().publisher(publisher); // Create a Uni from a Reactive Streams Publisher