Zip mono/flux with another that depends on the one I own

Viewed 293

I want to zip two monos/fluxes, but the second one (the one I'll zip) is dependand from the first that I already own.
For example:

//...

    fun addGroup(
        input: GroupInput
    ): Mono<Group> = Mono.just(Group(title = input.title, description = input.description))
        .flatMap { g -> groupsRepository.save(g) } // Gives me back the new db ID
        .zipWith(Mono.just(GroupMember(g.id /* <-- ??*/, input.ownerId, true)))
        //...

// ...

Is it possible?

3 Answers

I would say no. You can only zip things that can run in parallel.

you could use map or flatmap in this case, is there any reason you need to use Zip?

No, your code cannot even compile because variable g belongs to the lambda inside the flatMap() call. zipWith() is not intended for this use case, use map() or flatMap()

Related