Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RxJS:在一个可变数量的内部可观测值上进行顺序concat_Rxjs - Fatal编程技术网

RxJS:在一个可变数量的内部可观测值上进行顺序concat

RxJS:在一个可变数量的内部可观测值上进行顺序concat,rxjs,Rxjs,我需要连续观察一个可变数量的内部观测值,并在第一个观测值解析为给定结果时立即停止。 请参见下面的示例或我使用的concatMap,但这仅适用于固定数量的内部观察值 常量值=[1,4,6,3,9]; getRemoteValue(值[0])管道( concatMap(result=>result?of(result):getRemoteValue(值[1]), concatMap(result=>result?of(result):getRemoteValue(值[2]), concatMap(

我需要连续观察一个可变数量的内部观测值,并在第一个观测值解析为给定结果时立即停止。 请参见下面的示例或我使用的
concatMap
,但这仅适用于固定数量的内部观察值


常量值=[1,4,6,3,9];
getRemoteValue(值[0])管道(
concatMap(result=>result?of(result):getRemoteValue(值[1]),
concatMap(result=>result?of(result):getRemoteValue(值[2]),
concatMap(result=>result?of(result):getRemoteValue(值[3]),
concatMap(result=>result?of(result):getRemoteValue(值[4]))
).subscribe(success=>console.log(success?'foundit':'failed');
函数getRemoteValue(输入:数字):可观察{
log(`checking${input}`)
//在现实生活中,这将是一个异步远程调用
常数值=随机(10);
返回(值===输入);
}
从“lodash”导入*as uu;
从“rxjs”导入{可观察的,of,from,EMPTY};
从“rxjs/operators”导入{concatMap,single,find};
常量值=[1,4,6,3,9];
函数getRemoteValue(输入:数字):可观察{
log(`checking${input}`)
//在现实生活中,这将是一个异步远程调用
常数值=随机(10);
返回(值===输入);
}
从(值)。管道(
concatMap(getRemoteValue),
查找((v)=>typeof v=='boolean'&&v==true)
)
.订阅(成功=>{
console.log('success:',success);
log(成功?'foundit':'failed');
}
);

从“lodash”导入*as uu;
从“rxjs”导入{可观察的,of,from,EMPTY};
从“rxjs/operators”导入{concatMap,single,find};
常量值=[1,4,6,3,9];
函数getRemoteValue(输入:数字):可观察{
log(`checking${input}`)
//在现实生活中,这将是一个异步远程调用
常数值=随机(10);
返回(值===输入);
}
从(值)。管道(
concatMap(getRemoteValue),
查找((v)=>typeof v=='boolean'&&v==true)
)
.订阅(成功=>{
console.log('success:',success);
log(成功?'foundit':'failed');
}
);

很快我就和你谈过了,对不起。我错过了getRemoteValue函数不再返回可观察值,而是返回普通布尔值,使其同步。如果getRemoteValue返回一个可观察的值,我该怎么办呢?我很快就和你谈过了,对不起。我错过了getRemoteValue函数不再返回可观察值,而是返回普通布尔值,使其同步。如果getRemoteValue返回一个可观察值,我将如何处理?
import * as _ from 'lodash';
import { Observable, of, from, EMPTY } from 'rxjs';
import { concatMap, single, find } from 'rxjs/operators';

const values = [1, 4, 6, 3, 9];

function getRemoteValue(input: number): Observable<boolean> {
  console.log(`checking ${input}`)
  // this would be an async remote call in real life
  const value = _.random(10);
  return of(value === input);
}

from(values).pipe(
  concatMap(getRemoteValue),
  find((v) => typeof v === 'boolean' && v === true)
)
.subscribe(success => {
    console.log('success:', success);
    console.log(success ? 'found it' : 'failed');
  }
);