1
votes

I am making 10 independent network calls each returning a Mono of a different kind. These calls can run in parallel. After all of them finish, I want to compute something based on the results.

Mono.zip(
            mono1(),
            mono2(),
            mono3(),
            ...        
            mono8()
        )

Mono.zip allows me to compose a maximum of 8 Monos. I can do a workaround by using zip multiple times, but that isn't a clean way.

Mono.zip(
            mono1(),
            mono2(),
            mono3(),
            ...
            mono8()
        ).zipWith(mono9())

Overloads of zip that take array or iterable of Monos along with a combinator function give me Array<*> where I lose all my individual Mono types.

Are there any better ways to compose these many Monos ?

1
The mono tag is reserved for something else.Lex Li
Are you sure you want to use zip? it seems that reduce works better to compute based on the results.Igor Artamonov

1 Answers

1
votes

You can try to group your Monos based on some commonality into groups of less than 8 items and then zip those together.

Example:

Mono.zip(
        getLocationInfo(),
        getHotelInfo(),
        { locationInfo, hotelInfo -> Hotel(hotelInfo, locationInfo) }
)

fun getHotelInfo(): Mono<HotelInfo> {
    return Mono.zip(
            getStarRating(),
            getAvailableRooms(),
            { starRating, rooms -> HotelInfo(starRating, rooms) }
    )
}

fun getLocationInfo(): Mono<Region> {

    return Mono.zip(
            getCoordinates(),
            getCountry(),
            { coordinates, country -> Region(coordinates, country) }
    )
}