【问题标题】:How to keep the stream after a forkJoin or a concatAll(), toArray()如何在 forkJoin 或 concatAll()、toArray() 之后保留流
【发布时间】:2019-05-15 23:06:19
【问题描述】:

为了学习,我正在尝试在例如 forkjoin 之后保留流。但我想用管道(以一种优雅的方式)来做。不只是恢复数组,然后再重写一个from(array).subscribe。

我尝试在管道内做一个 from,但这不起作用。另外我想知道(作为奖励问题)在管道中(在这种情况下是第一个管道,因为我想会有 2 个管道)在 forkJoin 数组之前修改每个 mock_post 调用结果的理想方式是什么被退回。

只是我创建的一个小模拟函数,用于模拟 2 秒后的虚拟 api 返回值。

    private mock_post(url: string): Observable<object> {
        return from(timer(2000)).pipe(
            map(result => {
                return {id: 2, name: 'mock', source: url};
            })
        );
    }

使用 ForkJoin 的第一个测试用例(使用上述函数)



        const operations = [];
        operations.push(this.mock_post('/api/test/'));
        operations.push(this.mock_post('/api/test/2'));
        operations.push(this.mock_post('/api/test/3'));

        forkJoin(operations).pipe(

        )
            .subscribe(
                (next) => {
                    console.log(next);
                }
            );

concatAll() + toArray() 的第二个测试用例。

    const obs: Observable<any> = from(operations);
        obs.pipe(
            map(result => {
                // I want to modify the result here, and add something to the object or modify a property (for example)
                console.log(`result-> `, result); // This is(obviously, returning an observable, but how do I get the final value here?)

                result.name = 'mock_modified';

                return result;
            }),
            concatAll(),
            toArray()
        ).subscribe(
            (next) => {
                console.log(`-> NEXT`, next);
            },
            (error) => {
                console.log(`* ERROR`, error);
            },
            () => {
                console.log('=) COMPLETE');
            }
        );

如果有人能在这里散发出一些光芒,那就太好了。希望我的解释正确。

【问题讨论】:

    标签: typescript rxjs


    【解决方案1】:

    真的不需要用操作符做数组操作。你可以在地图上做

     forkJoin(operations).pipe(            
                map((results: any) => 
                   results.map(result=>({...result,name:'changed}))
    ),
    

    【讨论】:

      【解决方案2】:

      在搜索更多内容后,我正在回答我自己的问题。

      (顺便说一下我用的是RXJS6.5)

      对于 test1,在 forkjoin 之后,我基本上通过管道传输了一个 concatMap,并且在内部执行了一个 from(result),以从数组中发出每个值。之后,映射每个单独的结果,只测试返回的对象的变化并记录结果(如果不需要对对象进行修改,tap() 可能会达到相同的效果。

      之后,一个简单的 toArray() 将返回 1 个单个数组。

          test1() {
      
              const operations = [];
              operations.push(this.mock_post('/api/test/'));
              operations.push(this.mock_post('/api/test/2'));
              operations.push(this.mock_post('/api/test/3'));
      
              forkJoin(operations).pipe(
                  concatMap((result) => {
                      return from(result);
                  }),
                  map((result: any) => {
                      console.log(`resulting-> `, result);
                      result.name = 'changed';
                      return result;
                  }),
                  toArray()
              ).subscribe(
                  (next) => {
                      console.log(next);
                  }
              );
          }
      

      就是这么说的。如果有人有更好的方法,或者另一种方法。

      我全神贯注。

      我将这个问题标记为自己回答,因为它(目前)按我想要的方式工作。

      我仍在掌握 RXJS 的所有概念,并试图以一种优雅的方式保持流。

      干杯。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-05
        • 1970-01-01
        • 1970-01-01
        • 2019-02-12
        • 2017-11-18
        • 1970-01-01
        • 1970-01-01
        • 2022-01-02
        相关资源
        最近更新 更多