是否有等效于 doOnNext 但忽略任何反应结果(错误除外)?
Is there an equivalent to doOnNext but ignoring any reactive results (except errors)?
目前我正在做这个
final ReactiveHashOperations<String, String, String> ops = redisTemplate.opsForHash();
var theMonoIWant =
Mono.fromCallable(this::generateSomeComplexDataThatProducesTheValueIWant)
.flatMap(
theValueIWant -> {
... there is something here ...
return ops.putAll(somewhere, something)
.filter(success -> success)
.switchIfEmpty(
Mono.error(IllegalStateException::new)
)
.thenReturn(theValueIWant);
}
);
我也试过这个并且成功了,但我认为这可能是侥幸,因为我不知道完成订阅实际需要多长时间,并且可能存在竞争条件.
final ReactiveHashOperations<String, String, String> ops = redisTemplate.opsForHash();
var theMonoIWant =
Mono.fromCallable(this::generateSomeComplexDataThatProducesTheValueIWant)
.doOnNext(
theValueIWant -> {
ops.putAll(somewhere, something)
.filter(success -> success)
.switchIfEmpty(
Mono.error(IllegalStateException::new)
)
.subscribe();
}
);
但我正在寻找的是
var theMonoIWant =
Mono.fromCallable(this::generateSomeComplexDataThatProducesTheValueIWant)
.doSubscriptionOnNextButIgnoreTheReturnValueUnlessItsAnError(
theValueIWant ->
ops.putAll(somewhere, something)
.filter(success -> success)
.switchIfEmpty(
Mono.error(IllegalStateException::new)
);
);
不,第一个解决方案是惯用的。
“doSubscriptionOnNext”字面意思 flatMap
.
通过组合在内部发布者中更好地描述忽略来自所述内部订阅的值,而不是试图将所有可能的模式预测为 top-level flatMap 变体。
ignoreElements()
或您的情况 thenReturn
是在平面映射函数中实现该目标的好方法。
目前我正在做这个
final ReactiveHashOperations<String, String, String> ops = redisTemplate.opsForHash();
var theMonoIWant =
Mono.fromCallable(this::generateSomeComplexDataThatProducesTheValueIWant)
.flatMap(
theValueIWant -> {
... there is something here ...
return ops.putAll(somewhere, something)
.filter(success -> success)
.switchIfEmpty(
Mono.error(IllegalStateException::new)
)
.thenReturn(theValueIWant);
}
);
我也试过这个并且成功了,但我认为这可能是侥幸,因为我不知道完成订阅实际需要多长时间,并且可能存在竞争条件.
final ReactiveHashOperations<String, String, String> ops = redisTemplate.opsForHash();
var theMonoIWant =
Mono.fromCallable(this::generateSomeComplexDataThatProducesTheValueIWant)
.doOnNext(
theValueIWant -> {
ops.putAll(somewhere, something)
.filter(success -> success)
.switchIfEmpty(
Mono.error(IllegalStateException::new)
)
.subscribe();
}
);
但我正在寻找的是
var theMonoIWant =
Mono.fromCallable(this::generateSomeComplexDataThatProducesTheValueIWant)
.doSubscriptionOnNextButIgnoreTheReturnValueUnlessItsAnError(
theValueIWant ->
ops.putAll(somewhere, something)
.filter(success -> success)
.switchIfEmpty(
Mono.error(IllegalStateException::new)
);
);
不,第一个解决方案是惯用的。
“doSubscriptionOnNext”字面意思 flatMap
.
通过组合在内部发布者中更好地描述忽略来自所述内部订阅的值,而不是试图将所有可能的模式预测为 top-level flatMap 变体。
ignoreElements()
或您的情况 thenReturn
是在平面映射函数中实现该目标的好方法。