链接 3 个或更多依赖的 observables
Chain 3 or more dependent observables
我知道 之前有人问过这个问题。但是接受的解决方案对我不起作用或者我不能很好地理解它。
我正在使用 ng-7
我有简单的用例:
我有 2 个 API,第二个取决于第一个的响应。
我订阅了第一个 API 的结果,然后使用管道订阅了第二个 API 结果。
我的代码如下所示;
this._SomeService
.addUserToDb(payload)
.pipe(
map(res => res),
mergeMap(db1Response =>
this._SomeService.addUserToDb2(db1Response
)
),
catchError(errodb1 => {
return Observable.throw(new
Error(errorSso));
})
)
.subscribe(
resDb2 => {
// Here I get response of addUserToDb2
},
errDb2 => {
}
)
现在,在订阅第二个 API 响应之前,我想订阅另一个 observable 说:
this._tokenService.getToken.pipe(
)
并且想在服务 2 中使用它的响应。
这样:
API1 => 令牌 => API2
请建议如何实施。
更新:
我尝试实现了,下面是我的实现:
this._service.addUserToDB1(payload).pipe(
map(resp => this.resDB1 = resp) // Adding to global variable because I need this response while subscribing to DB2 service.
,mergeMap(resdb1=>this._tokenService.getToken.pipe(
mergeMap(token => this._service.addUserToDb2(
this.resDB1,
this.organizationId,
this.practitionerId,
token
),
catchError(errorToken => {
return Observable.throw(new Error(errorToken));
})),
)
),
catchError(errordb1 => {
return Observable.throw(new Error(errordb1));
})
).subscribe (
resdb2Response =>
{
},
errdb2 => {
}
)
有人可以验证以上实施是否正确或建议正确的方法吗?
mergeMap 运算符在这里很好,因为 Api 请求发出 1 个事件然后完成,但准确地说 请使用 switchMap 或 concatMap 而不是mergeMap.如果您有兴趣,请同时查看有关这些运算符的 post。 RxJs Mapping Operators: switchMap, mergeMap, concatMap
至于你的代码块,我建议类似的东西是:
this._service.addUserToDB1(payload).pipe(
catchError(errordb1 => {
// do something with error if you want
return Observable.throw(new Error(errordb1));
}),
tap(resp => this.resDB1 = resp),
switchMap(resdb1 => this._tokenService.getToken),
catchError(errorToken => {
// do something with error if you want
return Observable.throw(new Error(errorToken));
}),
switchMap(token => this._service.addUserToDb2(
this.resDB1,
this.organizationId,
this.practitionerId,
token
)),
catchError(errordb2 => {
// do something with error if you want
return Observable.throw(new Error(errordb2));
}),
).subscribe(
resdb2Response => {
},
anyError => {
// any of the errors will come here
}
)
tap()
运算符就像只是做某事而不更改发出的事件的任何内容。当您只想做某事而不是转换发出的事件时,更喜欢点击地图。
我知道
我正在使用 ng-7 我有简单的用例:
我有 2 个 API,第二个取决于第一个的响应。 我订阅了第一个 API 的结果,然后使用管道订阅了第二个 API 结果。
我的代码如下所示;
this._SomeService
.addUserToDb(payload)
.pipe(
map(res => res),
mergeMap(db1Response =>
this._SomeService.addUserToDb2(db1Response
)
),
catchError(errodb1 => {
return Observable.throw(new
Error(errorSso));
})
)
.subscribe(
resDb2 => {
// Here I get response of addUserToDb2
},
errDb2 => {
}
)
现在,在订阅第二个 API 响应之前,我想订阅另一个 observable 说:
this._tokenService.getToken.pipe(
)
并且想在服务 2 中使用它的响应。 这样:
API1 => 令牌 => API2
请建议如何实施。
更新:
我尝试实现了,下面是我的实现:
this._service.addUserToDB1(payload).pipe(
map(resp => this.resDB1 = resp) // Adding to global variable because I need this response while subscribing to DB2 service.
,mergeMap(resdb1=>this._tokenService.getToken.pipe(
mergeMap(token => this._service.addUserToDb2(
this.resDB1,
this.organizationId,
this.practitionerId,
token
),
catchError(errorToken => {
return Observable.throw(new Error(errorToken));
})),
)
),
catchError(errordb1 => {
return Observable.throw(new Error(errordb1));
})
).subscribe (
resdb2Response =>
{
},
errdb2 => {
}
)
有人可以验证以上实施是否正确或建议正确的方法吗?
mergeMap 运算符在这里很好,因为 Api 请求发出 1 个事件然后完成,但准确地说 请使用 switchMap 或 concatMap 而不是mergeMap.如果您有兴趣,请同时查看有关这些运算符的 post。 RxJs Mapping Operators: switchMap, mergeMap, concatMap
至于你的代码块,我建议类似的东西是:
this._service.addUserToDB1(payload).pipe(
catchError(errordb1 => {
// do something with error if you want
return Observable.throw(new Error(errordb1));
}),
tap(resp => this.resDB1 = resp),
switchMap(resdb1 => this._tokenService.getToken),
catchError(errorToken => {
// do something with error if you want
return Observable.throw(new Error(errorToken));
}),
switchMap(token => this._service.addUserToDb2(
this.resDB1,
this.organizationId,
this.practitionerId,
token
)),
catchError(errordb2 => {
// do something with error if you want
return Observable.throw(new Error(errordb2));
}),
).subscribe(
resdb2Response => {
},
anyError => {
// any of the errors will come here
}
)
tap()
运算符就像只是做某事而不更改发出的事件的任何内容。当您只想做某事而不是转换发出的事件时,更喜欢点击地图。