顺序订阅一系列可观察对象 [英] Sequential subscription to an array of observables

查看:64
本文介绍了顺序订阅一系列可观察对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我使用了rxjs中的forkJoin来并行订阅一个可观察对象数组.但是我想一个个地订阅它们,什么是最好的解决方案?

Here, I've used forkJoin from rxjs to subscribe to an array of observables parallelly. But I want to subscribe to them one by one, What will be the best solution?

下面是我的代码:

var observables = [];

Observable.forkJoin(observables)
    .subscribe(() => {
        this.msgs = [];
        this.msgs.push({
            severity: 'success',
            summary: 'Saved Successfully'
        });
        this.onSaveComplete();
    }, (error: any) => this.errorMessage = <any>error);
}, (error: any) => this.errorMessage = <any>error);

推荐答案

forkJoin的替代方案意味着您需要顺序预订可观察对象数组.在这种情况下,mergeconcat是可行的方法.在您的情况下,使用mergeconcat时,请修改您的代码并在可观察对象数组的最开始使用散布运算符.

Alternate of forkJoin means you need to subscribe to the array of observables sequencially. mergeand concat are the ways to go in this case. In your case, modify your code and use a spread operator at the very beginning of your array of observables when using mergeand concat.

var observables = [];
Observable.concat(...observables)
            .subscribe(() => {
                this.msgs = [];
                this.msgs.push({
                    severity: 'success',
                    summary: 'Saved Successfully'
                });
                this.onSaveComplete();
            }, (error: any) => this.errorMessage = <any>error);
    }, (error: any) => this.errorMessage = <any>error);

这篇关于顺序订阅一系列可观察对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆