Angular 角度rxjs concat 2个观察值并返回它们的元组
我有两个api调用需要一个接一个地同步进行。 我正在使用rxjsAngular 角度rxjs concat 2个观察值并返回它们的元组,angular,rxjs,Angular,Rxjs,我有两个api调用需要一个接一个地同步进行。 我正在使用rxjsconcat操作符来完成同步。 这非常有效,但是如何从两个观察值返回数据呢? 我就是这样实施的: const userId='someId'; this.restangular.one('users/generate-api-token').customPOST() // Will return an api token .concatMap(({token}) => this.restangular.one(`users
concat
操作符来完成同步。
这非常有效,但是如何从两个观察值返回数据呢?
我就是这样实施的:
const userId='someId';
this.restangular.one('users/generate-api-token').customPOST() // Will return an api token
.concatMap(({token}) => this.restangular.one(`users/${userId}/api-token-details`).get()) // Will return the token's detail
.map((data) => {
console.log(data) // I would like to get both token and details here
});
使用concatMap的resultSelector函数
Rx.Observable.of('123')
.concatMap(ev => Rx.Observable.of('abc'), (x,y) => [x,y])
.subscribe(x => console.log(x));
const userId='someId';
this.restangular.one('users/generate-api-token').customPOST() // Will return an api token
.switchMapTo(this.restangular.one(`users/${userId}/api-token-details`).get(), (apiToken, tokenDetails) => ({apiToken, tokenDetails})) // Will return the token's detail
.do(({apiToken, tokenDetails}) => {
console.log(apiToken, tokenDetails);
});