Return Flux 完成时的值?
Return value when Flux completes?
我想在 Flux
完成后 return 一个值(或发布者)。这是一个类似于我所追求的(伪)代码的示例:
val myId : Mono<String> = fetchMyId()
myId.flatMap { id ->
someFlux.map { .. }.doOnNext { ... }.returnOnComplete(Mono.just(id))
}
即我想在 someFlux
完成后 return id
。 returnOnComplete
函数是虚构的,不存在(有一个 doOnComplete
函数,但它是为了副作用)这就是我问这个问题的原因。我该怎么做?
您可以使用 then(Mono<V>)
运算符,因为它根据 the documentation:
完全符合您的要求
Let this Flux
complete then play signals from a provided Mono
.
In other words ignore element from this Flux
and transform its completion signal into the emission and completion signal of a provided Mono<V>
. Error signal is replayed in the resulting Mono<V>
.
例如:
Mono
.just("abc")
.flatMap(id -> Flux.range(1, 10)
.doOnNext(nr -> logger.info("Number: {}", nr))
.then(Mono.just(id)))
.subscribe(id -> logger.info("ID: {}", id));
在 then(Mono<V>)
之上,正如@g00glen00b 所建议的那样,当您的延续是一个简单的 Mono.just
:
mono.then(Mono.just("foo")); //is the same as:
mono.thenReturn("foo");
我想在 Flux
完成后 return 一个值(或发布者)。这是一个类似于我所追求的(伪)代码的示例:
val myId : Mono<String> = fetchMyId()
myId.flatMap { id ->
someFlux.map { .. }.doOnNext { ... }.returnOnComplete(Mono.just(id))
}
即我想在 someFlux
完成后 return id
。 returnOnComplete
函数是虚构的,不存在(有一个 doOnComplete
函数,但它是为了副作用)这就是我问这个问题的原因。我该怎么做?
您可以使用 then(Mono<V>)
运算符,因为它根据 the documentation:
Let this
Flux
complete then play signals from a providedMono
.In other words ignore element from this
Flux
and transform its completion signal into the emission and completion signal of a providedMono<V>
. Error signal is replayed in the resultingMono<V>
.
例如:
Mono
.just("abc")
.flatMap(id -> Flux.range(1, 10)
.doOnNext(nr -> logger.info("Number: {}", nr))
.then(Mono.just(id)))
.subscribe(id -> logger.info("ID: {}", id));
在 then(Mono<V>)
之上,正如@g00glen00b 所建议的那样,当您的延续是一个简单的 Mono.just
:
mono.then(Mono.just("foo")); //is the same as:
mono.thenReturn("foo");