使用from中包含變量,在運算躰中修改它,但是這個變量始終未改變爲啥,怎麽解決

const a = [1, 2, 3, 4, 5, 6, 7];

from(a.splice(0, 3))

  .pipe(

    concatMap(val => {

      return of(val).pipe(delay(Math.random() * 1000));

    }),

    tap(val => {

      console.log(val);

    }),

    repeatWhen(completed => completed.pipe(delay(2000))),

    tap(val => {

      console.log(a);

    }),

    takeWhile(val => a.length > 0)

  )

  .subscribe(() => {});

a.splice(0, 3)爲什麽永遠是[1,2,3],而不會變成[4,5,6]

主要原因是 repeat 不會重新再跑一次 Observable, 是重新 subscribe. 如果想達到你原本的目的


import { of, from } from 'rxjs';
import { mergeMap, repeat } from 'rxjs/operators';

const a = [1, 2, 3, 4];

of('a')
  .pipe(mergeMap(() => from(a.splice(0, 2))), repeat(2))
  .subscribe({
    next: (value) => console.log(value),
    complete: () => console.log(a),
  });

可以往這個方向思考