简体   繁体   English

Zip mono/flux 与另一个取决于我拥有的

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

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.我想要 zip 两个单声道/通量,但第二个(我将压缩的那个)依赖于我已经拥有的第一个。
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?是否可以?

I would say no.我会说不。 You can only zip things that can run in parallel.你只能并行运行 zip 个东西。

you could use map or flatmap in this case, is there any reason you need to use Zip?在这种情况下,您可以使用 map 或平面图,有什么理由需要使用 Zip 吗?

No, your code cannot even compile because variable g belongs to the lambda inside the flatMap() call.不,您的代码甚至无法编译,因为变量g属于flatMap()调用中的 lambda。 zipWith() is not intended for this use case, use map() or flatMap() zipWith()不适用于此用例,请使用map()flatMap()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM