withmarket-server/shop-review-reply

Occurred NoSuchElementException when using awaitSingle() at method returning Mono<Void> type

BrianDYKim opened this issue · 1 comments

Preview

As i mentioned, when using awaitSingle() method included in coroutines.reactor library, if that hangs with method that returns Mono type object, NoSuchElementException occurs.

Reason why is this situation caused?

Reason is very simple that I thought. Cuz if the Mono object is subscribed, this things generate empty object(Null), but awaitSingle() method does not allow null object!

So, we can solve this problem if I fix the usage of fooMono.awaitSingle()

Solution

First, suppose that fooMono is Mono.empty() that returns null when that is subscribed.

Then, fix like following code:

❗️from

val result = fooMono.awaitSingle()

👉to

val result = fooMono.awaitSingleOrNull()

Then, NoSuchElementException does not occur anymore.

solved.