Mono<Student> studentMono = some1();
Mono<School> schoolMono = some2();
Mono<Person> categoryBestPageResponseMono = Mono.zip(studentMono, schoolMono).flatMap(data -> {
Student student = data.getT1();
School school = data.getT2();
Person person = Person.builder()
.student(student)
.school(school)
.build();
return Mono.just(person);
}).flatMap(person -> {
Mono<PassInfo> passInfoMono= getPassOrfail(person.student.id, person.school.number);
//pass info is null when first and second get from cache not null
passInfoMono.subscribe(passInfo -> person.setPassInfo(passInfo));
return Mono.just(person);
});
在上面的源代码中,我总是为 passInfo 获取 null。
如何等待getPassOrfail操作,将passInfo放入setter中的personinfo中?
我会利用链接的优势:
...
.flatMap(person -> getPassOrfail(person.student.id, person.school.number)
.doOnSuccess(person::setPassInfo)
.thenReturn(person));
flatMap()
,我看到你正在使用它进行完全同步的操作,然后只是在 return 语句中执行 Mono.just(...)
。对于同步操作,有 .map()
运算符 Mono.zip(studentMono, schoolMono)
.map(data -> {
Student student = data.getT1();
School school = data.getT2();
Person person = Person.builder()
.student(student)
.school(school)
.build();
return person;
})
.flatMap(person -> getPassOrfail(person.getStudent().getId(), person.getSchool().getNumber())
.map(passInfo -> {
person.setPassInfo(passInfo);
return person;
})
);