public LoggedInUser$:Observable<any> = new Observable(observer => {
this._loggedInUser = observer;
this.api$.make_api_call('getapplication').subscribe(d => {
// do Something
});
});
[我如何锁定对此可观察对象的另一个API调用,该调用将是异步的,或者至少在getapplication
响应后启动?我尝试在其中添加另一个API调用,例如-
let params = {
'all_applications': true
};
this.api$.make_api_call('getallapplications', params).subscribe(d => {
// do Something
});
但是它不能解决我的目的,并且几乎同时进行两个调用。
这是两个单独的调用,彼此不依赖。想法是为用户引入单独的应用程序数据,完成后,运行异步调用(或可能在后台运行)以获取其余应用程序并将其缓存在Redis中。
您可以尝试concatMap吗? ConcatMap帮助您按顺序处理请求,即您首先获得响应,然后执行下一个调用。
import { of } from 'rxjs';
import { ajax } from 'rxjs/ajax';
import { map, concatMap } from 'rxjs/operators';
const users$ = ajax.getJSON('https://api.github.com/users');
users$.pipe(
concatMap(users => {
const userId = 1;
console.log('concat users', users)
return ajax.getJSON(`https://api.github.com/users/${userId}`)
})
).subscribe(user => console.log('user', user));
这里是stackblitz URL
根据您的情况,试试这个
this.api$.make_api_call('getapplication').pipe(
tap(response => console.log('First API response'),
concatMap(response => {
let params = {
'all_applications': true
};
return this.api$.make_api_call('getallapplications', params)
})
).subscribe(response => console.log('2nd API Response', response));