chain and flatMap?Yes, there is, although it’s small:
flatMap is used when the new Uni depends on the result of the previous one.
chain simply executes the next Uni without depending on the previous result.
Example:
// flatMap — the new Uni depends on the item
Uni<User> user = Uni.createFrom().item("123")
.flatMap(id -> fetchUserFromDatabase(id));
// chain — just executing the next action
Uni<Void> action = Uni.createFrom().item("123")
.chain(() -> sendAnalyticsEvent());
Yes, two important differences:
chain handles null gracefully, while flatMap throws a NullPointerException.
Uni<String> uni = Uni.createFrom().item("Hello");
uni.flatMap(item -\> null); // java.lang.NullPointerException: Mapper returned null
uni.chain(item -\> null); // Works, interpreted as an empty Uni
Error handling – both propagate exceptions, but chain is more predictable since it works sequentially.
To improve code readability. Other reactive libraries (Reactor, RxJava) only have flatMap, but Mutiny introduced chain to make it clear when you don’t need the result and just want to execute the next step.
Yes:
Use flatMap when you need to asynchronously transform data.
Use chain when you just need to execute the next operation without depending on the result.
Simple rule of thumb:
Need to work with the result? → flatMap
Just executing the next action? → chain
Expecting null? → chain
Yes. flatMap comes from functional programming and reactive libraries. Mutiny introduced chain to make code more readable when you don’t need the data and just want to execute the next step.