Processing Optional Value from Mono in Project Reactor
up vote
2
down vote
favorite
I want to have a Mono
that calls another async method that returns an Optional
type to:
- have a value if the
Optional
is not empty, - is
MonoEmpty
if theOptional
value is empty.
Here's what I do right now:
Mono.fromCallable(() -> someApi.asyncCall())
.filter(Optional::isPresent)
.map(Optional::get)
Obviously, this is not ideal since it uses two operators after the callable completed. If possible, I'd like to have the Mono.empty()
or mono value from inside fromCallable
.
What is the best way to achieve what I want?
java reactive-programming project-reactor
add a comment |
up vote
2
down vote
favorite
I want to have a Mono
that calls another async method that returns an Optional
type to:
- have a value if the
Optional
is not empty, - is
MonoEmpty
if theOptional
value is empty.
Here's what I do right now:
Mono.fromCallable(() -> someApi.asyncCall())
.filter(Optional::isPresent)
.map(Optional::get)
Obviously, this is not ideal since it uses two operators after the callable completed. If possible, I'd like to have the Mono.empty()
or mono value from inside fromCallable
.
What is the best way to achieve what I want?
java reactive-programming project-reactor
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I want to have a Mono
that calls another async method that returns an Optional
type to:
- have a value if the
Optional
is not empty, - is
MonoEmpty
if theOptional
value is empty.
Here's what I do right now:
Mono.fromCallable(() -> someApi.asyncCall())
.filter(Optional::isPresent)
.map(Optional::get)
Obviously, this is not ideal since it uses two operators after the callable completed. If possible, I'd like to have the Mono.empty()
or mono value from inside fromCallable
.
What is the best way to achieve what I want?
java reactive-programming project-reactor
I want to have a Mono
that calls another async method that returns an Optional
type to:
- have a value if the
Optional
is not empty, - is
MonoEmpty
if theOptional
value is empty.
Here's what I do right now:
Mono.fromCallable(() -> someApi.asyncCall())
.filter(Optional::isPresent)
.map(Optional::get)
Obviously, this is not ideal since it uses two operators after the callable completed. If possible, I'd like to have the Mono.empty()
or mono value from inside fromCallable
.
What is the best way to achieve what I want?
java reactive-programming project-reactor
java reactive-programming project-reactor
asked Nov 7 at 8:46
bertzzie
1,82832137
1,82832137
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
There is an alternative with flatMap
that's a bit better than Optional.isPresent
and Optional.get
that can lead to accidentally calling get on empty Optional
:
Mono.fromCallable(() -> someApi.asyncCall())
.flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))
Accepting this as it works. Thank you.
– bertzzie
Nov 8 at 2:01
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
There is an alternative with flatMap
that's a bit better than Optional.isPresent
and Optional.get
that can lead to accidentally calling get on empty Optional
:
Mono.fromCallable(() -> someApi.asyncCall())
.flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))
Accepting this as it works. Thank you.
– bertzzie
Nov 8 at 2:01
add a comment |
up vote
1
down vote
accepted
There is an alternative with flatMap
that's a bit better than Optional.isPresent
and Optional.get
that can lead to accidentally calling get on empty Optional
:
Mono.fromCallable(() -> someApi.asyncCall())
.flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))
Accepting this as it works. Thank you.
– bertzzie
Nov 8 at 2:01
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
There is an alternative with flatMap
that's a bit better than Optional.isPresent
and Optional.get
that can lead to accidentally calling get on empty Optional
:
Mono.fromCallable(() -> someApi.asyncCall())
.flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))
There is an alternative with flatMap
that's a bit better than Optional.isPresent
and Optional.get
that can lead to accidentally calling get on empty Optional
:
Mono.fromCallable(() -> someApi.asyncCall())
.flatMap(optional -> optional.map(Mono::just).orElseGet(Mono::empty))
answered Nov 7 at 11:21
Ilya Zinkovich
1,2401521
1,2401521
Accepting this as it works. Thank you.
– bertzzie
Nov 8 at 2:01
add a comment |
Accepting this as it works. Thank you.
– bertzzie
Nov 8 at 2:01
Accepting this as it works. Thank you.
– bertzzie
Nov 8 at 2:01
Accepting this as it works. Thank you.
– bertzzie
Nov 8 at 2:01
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186025%2fprocessing-optional-value-from-mono-in-project-reactor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown