智处理嵌套的流操作方式

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

Smart way to handle nested flux operations

问题

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

  1. private Mono<List<Student>> find(String name) {
  2. return repo.findByName(name)
  3. .flatMap((List<Student> students) -> {
  4. return repo.findAnotherName(anothName, 1).collectList()
  5. .flatMap((List<Student> anotherStudents) -> {
  6. //进行一些逻辑操作
  7. return Mono.just(students);
  8. });
  9. });
  10. }
英文:

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:

  1. private Mono&lt;List&lt;Student&gt;&gt; find(String name) {
  2. return repo.findByName(name)
  3. .flatMap((List&lt;Student&gt; students) -&gt; {
  4. return repo.findAnotherName(anothName, 1).collectList()
  5. .flatMap((List&lt;Student&gt; anotherStudents) -&gt; {
  6. //do some logic
  7. return Mono.just(students);
  8. });
  9. });
  10. }

Thanks in advance.

答案1

得分: 0

以下是您要翻译的内容:

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

  1. private Mono<List<Student>> find(String name) {
  2. return repo.findByName(name)
  3. .zipWhen((List<Student> students) -> {
  4. return repo.findAnotherName(anothName, 1).collectList();
  5. }, (students, anotherStudents) -> {
  6. //一些同步逻辑
  7. return students;
  8. });
  9. }

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

  1. private Mono<List<Student>> find(String name) {
  2. return repo.findByName(name)
  3. .zipWhen((List<Student> students) -> {
  4. return repo.findAnotherName(anothName, 1).collectList()
  5. .flatMap(anotherStudents -> someAsyncMethod(anotherStudents));
  6. }, ((students, o) -> students));
  7. }
英文:

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

  1. private Mono&lt;List&lt;Student&gt;&gt; find(String name) {
  2. return repo.findByName(name)
  3. .zipWhen((List&lt;Student&gt; students) -&gt; {
  4. return repo.findAnotherName(anothName, 1).collectList();
  5. }, (students, anotherStudents) -&gt; {
  6. //some sync logic
  7. return students;
  8. });
  9. }

But if the logic is also async, then

  1. private Mono&lt;List&lt;Student&gt;&gt; find(String name) {
  2. return repo.findByName(name)
  3. .zipWhen((List&lt;Student&gt; students) -&gt; {
  4. return repo.findAnotherName(anothName, 1).collectList()
  5. .flatMap(anotherStudents -&gt; someAsyncMethod(anotherStudents));
  6. }, ((students, o) -&gt; students));
  7. }

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

如何将ArrayList中的字符串值存储到一个ArrayList的对象中。 go 96
匿名

发表评论

匿名网友

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

确定

  • 开发者交流平台

    本页二维码