Background difference between transform() and trasformToUni() #23481
-
So mongoService.retrieveClothesList(clothesDto) is an async action. Noticed that onItem().transformToUni() is equivalent to flatmap() public Uni<List<Clothes>> retrieve(ClothesDto clothesDto) {
logger.info("ClothesDto: " + clothesDto);
return mongoService
.retrieveClothesList(clothesDto)
.onItem()
.transformToUni(this::handleListResponse);
}
private Uni<List<Clothes>> handleListResponse(List<Clothes> clothes) {
if (Objects.isNull(clothes)) {
logger.info("Mongo failed to retrieve clothes list");
return Uni.createFrom().nullItem();
}
logger.info(clothes.toString());
return Uni.createFrom().item(clothes);
} Although, i have a question that will help me deeply understand the async meaning and the response handling of this action. public Uni<List<Clothes>> retrieve(ClothesDto clothesDto) {
logger.info("ClothesDto: " + clothesDto);
return mongoService
.retrieveClothesList(clothesDto)
.onItem()
.transform(this::handleListResponse);
}
private List<Clothes> handleListResponse(List<Clothes> clothes) {
if (Objects.isNull(clothes)) {
logger.info("Mongo failed to retrieve clothes list");
return null;
}
logger.info(clothes.toString());
return clothes;
} The output from mongo service has calculated because of uni, but is it necessary to pass the output and convert the whole method to Uni? Thank you in advance |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
/cc @jponge |
Beta Was this translation helpful? Give feedback.
-
I didn't get what was your exact question, but I'll explain here the difference between the 2 methods.
For instance if operation
See https://smallrye.io/smallrye-mutiny/getting-started/transforming-items and https://smallrye.io/smallrye-mutiny/getting-started/transforming-items-async |
Beta Was this translation helpful? Give feedback.
I didn't get what was your exact question, but I'll explain here the difference between the 2 methods.
transform
does a synchronous transformation, and you should use it for anything that does not have I/O involved.transformToUni
should be used when you want to chain the processing with an operation that does some async I/O.For instance if operation
A
gives you an objectfoo
:transform
if you do something likefoo -> foo.getName()
transformToUni
if you do something likefoo -> db.insert(foo)
.See https://smallrye.io/smallrye-mutiny/getting-started/transforming-items and https://smallrye.io/smallrye-mutiny/getting-started/transforming-items-async