Changing http-requests conditionally
up vote
1
down vote
favorite
I'm new to RXJS and I have a working piece of code for http-request and subscribing to Observables from a shared service in Angular. This works, but I'd want to get rid of the nested subscriptions, which I know is probably very possible with RXJS operators, but I haven't figured out how. The usage of flatMap
in the nested request wouldn't be necessary, as the second nested request isn't dependable on the first.
this.sub = this.service.isPending$
.subscribe(data => {
if(data) { // gives boolean
this.service.getPending()
.subscribe((data:Offer) => this.offer = data)
} else {
this.sub2 = this.service.amount$
.flatMap((data: Amount) => {
this.amount = data;
return this.service.getOffer()
})
.subscribe((data: Offer) => {
this.offer = data;
})
}
})
How would I go about cleaning this code up? I'm using RXJS: "^5.0.1"
typescript angular-2+ rxjs
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
1
down vote
favorite
I'm new to RXJS and I have a working piece of code for http-request and subscribing to Observables from a shared service in Angular. This works, but I'd want to get rid of the nested subscriptions, which I know is probably very possible with RXJS operators, but I haven't figured out how. The usage of flatMap
in the nested request wouldn't be necessary, as the second nested request isn't dependable on the first.
this.sub = this.service.isPending$
.subscribe(data => {
if(data) { // gives boolean
this.service.getPending()
.subscribe((data:Offer) => this.offer = data)
} else {
this.sub2 = this.service.amount$
.flatMap((data: Amount) => {
this.amount = data;
return this.service.getOffer()
})
.subscribe((data: Offer) => {
this.offer = data;
})
}
})
How would I go about cleaning this code up? I'm using RXJS: "^5.0.1"
typescript angular-2+ rxjs
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm new to RXJS and I have a working piece of code for http-request and subscribing to Observables from a shared service in Angular. This works, but I'd want to get rid of the nested subscriptions, which I know is probably very possible with RXJS operators, but I haven't figured out how. The usage of flatMap
in the nested request wouldn't be necessary, as the second nested request isn't dependable on the first.
this.sub = this.service.isPending$
.subscribe(data => {
if(data) { // gives boolean
this.service.getPending()
.subscribe((data:Offer) => this.offer = data)
} else {
this.sub2 = this.service.amount$
.flatMap((data: Amount) => {
this.amount = data;
return this.service.getOffer()
})
.subscribe((data: Offer) => {
this.offer = data;
})
}
})
How would I go about cleaning this code up? I'm using RXJS: "^5.0.1"
typescript angular-2+ rxjs
I'm new to RXJS and I have a working piece of code for http-request and subscribing to Observables from a shared service in Angular. This works, but I'd want to get rid of the nested subscriptions, which I know is probably very possible with RXJS operators, but I haven't figured out how. The usage of flatMap
in the nested request wouldn't be necessary, as the second nested request isn't dependable on the first.
this.sub = this.service.isPending$
.subscribe(data => {
if(data) { // gives boolean
this.service.getPending()
.subscribe((data:Offer) => this.offer = data)
} else {
this.sub2 = this.service.amount$
.flatMap((data: Amount) => {
this.amount = data;
return this.service.getOffer()
})
.subscribe((data: Offer) => {
this.offer = data;
})
}
})
How would I go about cleaning this code up? I'm using RXJS: "^5.0.1"
typescript angular-2+ rxjs
typescript angular-2+ rxjs
edited Dec 7 '17 at 3:00
Jamal♦
30.2k11115226
30.2k11115226
asked Oct 5 '17 at 12:36
DRNR
91
91
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
On Observables
My personal preference is to have as few Subscription
objects and subscribe()
invocations as possible. If I don't have mistakes in the refactored code, you can achieve a single subscribe()
which does the same things as your code. I also got rid of sub2
.
this.offerSubscription = this.service
.isPending$
.flatMap(data => {
this.amount = null;
const offerObservable = data ?
this.service.getPending() :
this.service
.amount$
.flatMap((data: Amount) => {
this.amount = data;
return this.service.getOffer();
});
return offerObservable;
})
.subscribe((offer: Offer) => this.offer = offer);
On TypeScript
sub
, sub2
, isPending$
and data
are pretty bad names because they don't describe the entities they hold. I recommend changing them.
At the bare minimum, specify the type of the input parameter in your arrow function to make the code more readable: (data: Offer) => {...}
or better (receivedOffer: Offer) => { ... }
.
Specifying the type of a callback parameter is very much like an implicit type assertion and masks the introduction ofany
.
– Aluan Haddad
Mar 12 at 9:45
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
On Observables
My personal preference is to have as few Subscription
objects and subscribe()
invocations as possible. If I don't have mistakes in the refactored code, you can achieve a single subscribe()
which does the same things as your code. I also got rid of sub2
.
this.offerSubscription = this.service
.isPending$
.flatMap(data => {
this.amount = null;
const offerObservable = data ?
this.service.getPending() :
this.service
.amount$
.flatMap((data: Amount) => {
this.amount = data;
return this.service.getOffer();
});
return offerObservable;
})
.subscribe((offer: Offer) => this.offer = offer);
On TypeScript
sub
, sub2
, isPending$
and data
are pretty bad names because they don't describe the entities they hold. I recommend changing them.
At the bare minimum, specify the type of the input parameter in your arrow function to make the code more readable: (data: Offer) => {...}
or better (receivedOffer: Offer) => { ... }
.
Specifying the type of a callback parameter is very much like an implicit type assertion and masks the introduction ofany
.
– Aluan Haddad
Mar 12 at 9:45
add a comment |
up vote
0
down vote
On Observables
My personal preference is to have as few Subscription
objects and subscribe()
invocations as possible. If I don't have mistakes in the refactored code, you can achieve a single subscribe()
which does the same things as your code. I also got rid of sub2
.
this.offerSubscription = this.service
.isPending$
.flatMap(data => {
this.amount = null;
const offerObservable = data ?
this.service.getPending() :
this.service
.amount$
.flatMap((data: Amount) => {
this.amount = data;
return this.service.getOffer();
});
return offerObservable;
})
.subscribe((offer: Offer) => this.offer = offer);
On TypeScript
sub
, sub2
, isPending$
and data
are pretty bad names because they don't describe the entities they hold. I recommend changing them.
At the bare minimum, specify the type of the input parameter in your arrow function to make the code more readable: (data: Offer) => {...}
or better (receivedOffer: Offer) => { ... }
.
Specifying the type of a callback parameter is very much like an implicit type assertion and masks the introduction ofany
.
– Aluan Haddad
Mar 12 at 9:45
add a comment |
up vote
0
down vote
up vote
0
down vote
On Observables
My personal preference is to have as few Subscription
objects and subscribe()
invocations as possible. If I don't have mistakes in the refactored code, you can achieve a single subscribe()
which does the same things as your code. I also got rid of sub2
.
this.offerSubscription = this.service
.isPending$
.flatMap(data => {
this.amount = null;
const offerObservable = data ?
this.service.getPending() :
this.service
.amount$
.flatMap((data: Amount) => {
this.amount = data;
return this.service.getOffer();
});
return offerObservable;
})
.subscribe((offer: Offer) => this.offer = offer);
On TypeScript
sub
, sub2
, isPending$
and data
are pretty bad names because they don't describe the entities they hold. I recommend changing them.
At the bare minimum, specify the type of the input parameter in your arrow function to make the code more readable: (data: Offer) => {...}
or better (receivedOffer: Offer) => { ... }
.
On Observables
My personal preference is to have as few Subscription
objects and subscribe()
invocations as possible. If I don't have mistakes in the refactored code, you can achieve a single subscribe()
which does the same things as your code. I also got rid of sub2
.
this.offerSubscription = this.service
.isPending$
.flatMap(data => {
this.amount = null;
const offerObservable = data ?
this.service.getPending() :
this.service
.amount$
.flatMap((data: Amount) => {
this.amount = data;
return this.service.getOffer();
});
return offerObservable;
})
.subscribe((offer: Offer) => this.offer = offer);
On TypeScript
sub
, sub2
, isPending$
and data
are pretty bad names because they don't describe the entities they hold. I recommend changing them.
At the bare minimum, specify the type of the input parameter in your arrow function to make the code more readable: (data: Offer) => {...}
or better (receivedOffer: Offer) => { ... }
.
edited Oct 8 '17 at 1:28
answered Oct 7 '17 at 22:44
Igor Soloydenko
2,773928
2,773928
Specifying the type of a callback parameter is very much like an implicit type assertion and masks the introduction ofany
.
– Aluan Haddad
Mar 12 at 9:45
add a comment |
Specifying the type of a callback parameter is very much like an implicit type assertion and masks the introduction ofany
.
– Aluan Haddad
Mar 12 at 9:45
Specifying the type of a callback parameter is very much like an implicit type assertion and masks the introduction of
any
.– Aluan Haddad
Mar 12 at 9:45
Specifying the type of a callback parameter is very much like an implicit type assertion and masks the introduction of
any
.– Aluan Haddad
Mar 12 at 9:45
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2fcodereview.stackexchange.com%2fquestions%2f177216%2fchanging-http-requests-conditionally%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