Sobes.tech
Senior

What is the difference between creating Mono via just and fromCallable?

sobes.tech AI

Answer from AI

Mono.just(value) creates a Mono that immediately contains the given value. The value is computed and passed to just synchronously at creation.

Mono.fromCallable(callable) creates a Mono that, upon subscription, invokes the provided Callable and emits its result. This allows deferring the computation until subscription and handling potential exceptions.

Example:

Mono<String> monoJust = Mono.just(expensiveComputation()); // computation occurs immediately

Mono<String> monoFromCallable = Mono.fromCallable(() -> expensiveComputation()); // computation upon subscription

Use fromCallable if the computation is resource-intensive or may throw an exception, and you want to defer it until subscription.