admin管理员组文章数量:1398831
I need to call two http service calls and subscribe them after subscribing another one http service call. I mean, the operation will be sequential as the last two calls depend on the first one. Can this operation be handled with concatMap of RxJs?
I solved the problem using nested subscription. But, I think using concatMap, this can be done. In all examples I searched, there are two calls which are concatenated. How to concat more than two subscription and solve the issue.
this.accountService.fetchBranchCodeLength().subscribe(
data => {
this.branchCodeLength = +data;
//Now, I need to call another service to calculate
accountNumberLengthWithProductCode//
this.subscribers.fetchAcoountNumberLength =
this.accountService.fetchAccountLengthConfiguration().subscribe(
accountLength => {
this.accountNumberLengthWithProductCode =
(+accountLength) - (+this.branchCodeLength);});
//Another same kind of call to calculate
SUBGLCodeLength//
this.subscribers.fetchSUBGLCodeLengthSub =
this.glAccountService.fetchSUBGlCodeLength(SUBGlCodeQueryParam)
.subscribe(length => this.SUBGLCodeLength =
(+length.value) - (+this.branchCodeLength)
);
}
);
I need to call two http service calls and subscribe them after subscribing another one http service call. I mean, the operation will be sequential as the last two calls depend on the first one. Can this operation be handled with concatMap of RxJs?
I solved the problem using nested subscription. But, I think using concatMap, this can be done. In all examples I searched, there are two calls which are concatenated. How to concat more than two subscription and solve the issue.
this.accountService.fetchBranchCodeLength().subscribe(
data => {
this.branchCodeLength = +data;
//Now, I need to call another service to calculate
accountNumberLengthWithProductCode//
this.subscribers.fetchAcoountNumberLength =
this.accountService.fetchAccountLengthConfiguration().subscribe(
accountLength => {
this.accountNumberLengthWithProductCode =
(+accountLength) - (+this.branchCodeLength);});
//Another same kind of call to calculate
SUBGLCodeLength//
this.subscribers.fetchSUBGLCodeLengthSub =
this.glAccountService.fetchSUBGlCodeLength(SUBGlCodeQueryParam)
.subscribe(length => this.SUBGLCodeLength =
(+length.value) - (+this.branchCodeLength)
);
}
);
Share
Improve this question
edited Jan 30, 2019 at 6:54
Shofol
asked Jan 30, 2019 at 6:45
ShofolShofol
7331 gold badge11 silver badges26 bronze badges
4
-
1
Yes, you can use
concatMap
; see stackoverflow./questions/40651060/… – xkeshav Commented Jan 30, 2019 at 6:53 - Possible duplicate of How to use RxJS observables in sequential order? – Harun Yilmaz Commented Jan 30, 2019 at 6:58
- Possible duplicate of Angular 2 bine three http calls with flatMap? RxJs? – Bunyamin Coskuner Commented Jan 30, 2019 at 7:05
- According to your links, I've changed the code like this but how to call another service now? this.subscribers.fetchBranchCodeLengthSub = this.accountService.fetchBranchCodeLength().pipe( concatMap(data => this.accountService.fetchAccountLengthConfiguration().pipe( map(accountLength => { this.accountNumberLengthWithProductCode = (+accountLength) - (+data); }) ) ) ).subscribe(); – Shofol Commented Jan 30, 2019 at 8:03
1 Answer
Reset to default 7Concat map is used when you have a lot of requests, but when they are similar, it means they will have the same output data and handler, which is not your case. So you can use forkJoin, because there are no any dependencies among your calls.
Code
forkJoin(
this.accountService.fetchBranchCodeLength(),
this.accountService.fetchAccountLengthConfiguration(),
this.glAccountService.fetchSUBGlCodeLength(SUBGlCodeQueryParam)
).subscribe(([data, accountLength, length]) => {
this.branchCodeLength = +data;
this.accountNumberLengthWithProductCode = (+accountLength) - this.branchCodeLength
this.SUBGLCodeLength = (+length.value) - this.branchCodeLength;
});
And by the way, you do not need to unsubscribe from http call observables, because they are finite (to be precise the emit only one event).
本文标签: javascriptHow to manage multiple sequential subscribe methods in Angular and RxJsStack Overflow
版权声明:本文标题:javascript - How to manage multiple sequential subscribe methods in Angular and RxJs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744124894a2591911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论