智处理嵌套的流操作方式

huangapple go评论66阅读模式
英文:

Smart way to handle nested flux operations

问题

以下是代码段的翻译结果:

private Mono<List<Student>> find(String name) {

    return repo.findByName(name)
            .flatMap((List<Student> students) -> {

                return repo.findAnotherName(anothName, 1).collectList()
                        .flatMap((List<Student> anotherStudents) -> {

                            //进行一些逻辑操作

                            return Mono.just(students);
                        });

            });

}
英文:

I have 2 query methods (findByName/findAnotherName) .

Both of them return Mono<List<Student>> .

I do some logic by compare results of these two methods, and then return one of them in a nested Flux operation.

It may have a smart way to achieve same result though.

Following is code snippet:

private Mono&lt;List&lt;Student&gt;&gt; find(String name) {

	return repo.findByName(name)
			.flatMap((List&lt;Student&gt; students) -&gt; {

				return repo.findAnotherName(anothName, 1).collectList()
						.flatMap((List&lt;Student&gt; anotherStudents) -&gt; {

							//do some logic

							return Mono.just(students);
						});

			});

}

Thanks in advance.

答案1

得分: 0

以下是您要翻译的内容:

如果您的 //进行一些逻辑 是同步的,那么我可以提供类似这样的建议:

private Mono<List<Student>> find(String name) {

    return repo.findByName(name)
            .zipWhen((List<Student> students) -> {
                return repo.findAnotherName(anothName, 1).collectList();
            }, (students, anotherStudents) -> {
                //一些同步逻辑
                return students;
            });
}

但如果逻辑也是异步的,那么:

private Mono<List<Student>> find(String name) {

    return repo.findByName(name)
            .zipWhen((List<Student> students) -> {
                return repo.findAnotherName(anothName, 1).collectList()
                        .flatMap(anotherStudents -> someAsyncMethod(anotherStudents));
            }, ((students, o) -> students));
}
英文:

https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#zipWhen-java.util.function.Function-java.util.function.BiFunction-

If your //do some logic is sync then I can suggest something like

private Mono&lt;List&lt;Student&gt;&gt; find(String name) {

    return repo.findByName(name)
            .zipWhen((List&lt;Student&gt; students) -&gt; {
                return repo.findAnotherName(anothName, 1).collectList();
            }, (students, anotherStudents) -&gt; {
                //some sync logic
                return students;
            });
}

But if the logic is also async, then

private Mono&lt;List&lt;Student&gt;&gt; find(String name) {

    return repo.findByName(name)
            .zipWhen((List&lt;Student&gt; students) -&gt; {
                return repo.findAnotherName(anothName, 1).collectList()
                        .flatMap(anotherStudents -&gt; someAsyncMethod(anotherStudents));
            }, ((students, o) -&gt; students));
}

huangapple
  • 本文由 发表于 2020年10月7日 17:33:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64241208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定