链接请求的RxJS forkJoin



我有

const jobsToRun = this.config.data.tests.map(test => test.location);
const jobsSubmits: any[] = jobsToRun
.map(job => this.jobsService.submitTestJob(job).pipe(first())); // HERE
const finito = await forkJoin(jobsSubmits).toPromise();

所以我并行提交测试,并等待所有提交完成。我想在某种程度上改进这一点,提交测试,并使用响应(提交(中的数据更新数据库(另一个调用(。

如何最好地实现这个forkJoin(submitTest->在数据库中记录提交情况(?我想我只是不知道哪个RxJS操作符会是理想的。

在解决finito之后进行是不可行的,我需要在提交单独的测试后尽快将记录保存在数据库中。

将第一次调用的结果转换为第二次调用应该很简单。

类似这样的东西:

const jobsSubmits = this.config.data.tests
.map(test => test.location)
.map(job => this.jobsService.submitTestJob(job).pipe(
first(),
switchMap(submitted => this.jobsService.makeRecordABout(submitted))
));
const finito = await forkJoin(jobsSubmits).toPromise();

我假设submitTestJob是一个远程http调用,它返回一个Observable。如果这是真的,我会做一些类似的事情

const jobsSubmits = jobsToRun
.map(job => this.jobsService.submitTestJob(job).pipe(
// if submitTestJob is an http call, then first should not be necessary
first(),
// addRecord is a function which returns an Observable which emits when
// the record is added
concatMap(submission => addRecord(submission))
)
);

在本文中,您可能会阅读到更多关于使用http的典型rxjs模式的信息。

最新更新