Dachstein
Dachstein

Reputation: 4282

Spring Reactor - Wait until a mono is finished then do next Mono

Lets say I have a repository.save(..) method which returns a Mono.

Also lets say I have a repository.findByEmail(..) which returns a Mono.

Problem:

I want that the first Mono finishes AFTER do the second Mono.

repository.save(..).then(repository.findByEmail(..))

However, the second Mono here always gets executed first? I was under the impression that .then(..) finishes and then plays another Mono

The source code says:
Let this {@link Mono} complete then play another Mono.

What is the solution to my problem?

Upvotes: 6

Views: 18482

Answers (1)

Brian Clozel
Brian Clozel

Reputation: 59086

What makes you think that this operator doesn't behave as expected?

The following example shows it does:

Mono.just("first").log()
                .then(Mono.just("second")).log()
                .subscribe();

Logs:

[main] INFO  reactor.Mono.IgnoreThen.2 - | onSubscribe([Fuseable] MonoIgnoreThen.ThenIgnoreMain)
[main] INFO  reactor.Mono.IgnoreThen.2 - | request(unbounded)
[main] INFO  reactor.Mono.Just.1 - | onSubscribe([Synchronous Fuseable] Operators.ScalarSubscription)
[main] INFO  reactor.Mono.Just.1 - | request(unbounded)
[main] INFO  reactor.Mono.Just.1 - | onNext(first)
[main] INFO  reactor.Mono.Just.1 - | onComplete()
[main] INFO  reactor.Mono.IgnoreThen.2 - | onNext(second)
[main] INFO  reactor.Mono.IgnoreThen.2 - | onComplete()

Please add log operators and share the logs in your question.

Upvotes: 8

Related Questions