[英]Make 2nd API call in the background after 1st one ends in an observable
public LoggedInUser$:Observable<any> = new Observable(observer => {
this._loggedInUser = observer;
this.api$.make_api_call('getapplication').subscribe(d => {
// do Something
});
});
How do I latch on another API call to this observable that'd be asynchronous or at least fired up after the response of getapplication
?我如何锁定另一个 API 调用这个异步的或至少在
getapplication
响应后启动的可观察对象? I tried adding in another API call inside like -我尝试在内部添加另一个 API 调用,例如 -
let params = {
'all_applications': true
};
this.api$.make_api_call('getallapplications', params).subscribe(d => {
// do Something
});
but it doesn't solve my purpose and both the calls are made almost simultaneously.但这并没有解决我的目的,而且这两个电话几乎是同时进行的。
These are two separate calls and not dependent on each other.这是两个独立的调用,彼此不依赖。 Idea is to bring in individual application data for the user and once that's done, running an async call (or maybe something in the background) to get rest of the applications and cache it in Redis.
想法是为用户引入单独的应用程序数据,一旦完成,运行异步调用(或者可能在后台调用)以获取 rest 的应用程序并将其缓存在 Redis 中。
The requirement is to bring in a single application data for a customer.要求是为客户引入单个应用程序数据。 At the same time bringing in all the other applications data so that when user switches between it, user experience doesn't suffer.
同时引入所有其他应用程序数据,以便当用户在它们之间切换时,用户体验不会受到影响。
Can you try concatMap ?你能试试concatMap吗? ConcatMap helps you to process the requests in sequence ie ones you get the response for first your next call gets executed.
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));
Here is the stackblitz URL这是 stackblitz URL
concatMap RXJS example concatMap RXJS 示例
In your case, try this在你的情况下,试试这个
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.