简体   繁体   English

在第一个结束后在后台进行第二个 API 调用

[英]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.

相关问题 当第二个中的第一个可观测值需要时,如何在angular2中链接两个可观测的对象 - How to chain two observables in angular2, when value of 1st observable need in 2nd one 如何使用第一个 API 通话中的数据并在我的第二个 API 通话中使用该数据? - How to use a data from the 1st API call and use that in my 2nd API call? 从第一张背景图片到第二张背景图片5秒后淡入负载 - Fade in on load after 5 seconds from 1st background image to 2nd background image 角异步诺言未正确链接以回馈第一个调用以进行第二个调用 - Angular async promise is not chaining correctly to give back 1st call in order to make 2nd call 在第一个json调用正常工作但第二个json调用不工作直到页面刷新 - After 1st json call working but 2nd json call not working till page get refresh 如何在第一个异步方法完成后运行第二个异步方法 - How to make 2nd async method run after 1st async method has finished 如何在第二次之后停止第一次 function? - How to stop doing 1st function after 2nd? 显示第一个 div 并隐藏其余部分,然后几秒钟后通过隐藏其余部分(包括第一个 div)来显示第二个 div - display one 1st div and hide rest then after few seconds display 2nd div by hiding rest of them including 1st div 我这里有 2 个表格,在提交验证第一个表格后,我如何移动到第二个表格进行验证? - I have 2 forms here,after submitting validatting the 1st one, how can I move to the 2nd to validate? 从第一个填充第二个列表框 - populate 2nd listbox from 1st
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM