如何迭代返回rxjs可观测值的函数

如何迭代返回rxjs可观测值的函数,rxjs,rxjs-observables,Rxjs,Rxjs Observables,我想迭代一系列异步函数,并在返回false时结束迭代。 我是新来的rxjs,无法使用下面的用例。我觉得我没有理解一些基本的东西。有人能给我指一下吗 function validateA(): Observable<any> { // do stuff. return of({ id: "A", result: true }); // hardcoding result for now } function validateB(): Observable&

我想迭代一系列异步函数,并在返回false时结束迭代。 我是新来的
rxjs
,无法使用下面的用例。我觉得我没有理解一些基本的东西。有人能给我指一下吗

function validateA(): Observable<any> {
  // do stuff.
  return of({ id: "A", result: true }); // hardcoding result for now
}

function validateB(): Observable<any> {
  // do stuff
  return of({ id: "B", result: true }); // hardcoding result for now
}

function validateC(): Observable<any> {
  // do stuff
  return of({ id: "C", result: false });// hardcoding result for now
}

from([validateA, validateB, validateC])
  .pipe(
    map(data => data()),
    takeWhile(data => !!data.result)
  )
  .subscribe(data => console.log(`${data.id} passed!`));

function validateA():Observable

下面的代码似乎起到了作用,并懒洋洋地调用函数:


我想说你的逻辑核心是正确的。缺少的是rxJs的一些特殊性

解决方案可能是这样的。评论中对细微差别进行了解释

// start from an array of functions and turn it into a stream using RxJs from function
from([validateA, validateB, validateC])
  .pipe(
    // now execute each function sequentially, one after the other, via concatMap
    // operator. This operator calls each function and each function returns an Observable
    // concatMap ensures that the functions are called sequentially and also that the returned Observable (because each function returns an Observable)
    // is "flattened" in the result stream. In other words, you execute each function one at the time
    // and return the value emitted by the Observable returned by that function
    // until that Observable completes. Considering that you use the "of" function to
    // create the Observable which is returned by each function, such Observable emits just one value and then completes.
    concatMap(func => func()),
    // now you have a stream of values notified by the Observables returned by the functions
    // and you terminate as soon as a flase is received
    takeWhile(data => !!data.result)
  )
  .subscribe(data => console.log(`${data.id} passed!`));
// start from an array of functions and turn it into a stream using RxJs from function
from([validateA, validateB, validateC])
  .pipe(
    // now execute each function sequentially, one after the other, via concatMap
    // operator. This operator calls each function and each function returns an Observable
    // concatMap ensures that the functions are called sequentially and also that the returned Observable (because each function returns an Observable)
    // is "flattened" in the result stream. In other words, you execute each function one at the time
    // and return the value emitted by the Observable returned by that function
    // until that Observable completes. Considering that you use the "of" function to
    // create the Observable which is returned by each function, such Observable emits just one value and then completes.
    concatMap(func => func()),
    // now you have a stream of values notified by the Observables returned by the functions
    // and you terminate as soon as a flase is received
    takeWhile(data => !!data.result)
  )
  .subscribe(data => console.log(`${data.id} passed!`));