【发布时间】:2016-12-27 05:41:36
【问题描述】:
我猜这应该很容易实现,但我在弄清楚如何解决它时遇到了麻烦(我猜是概念上的)。
我拥有的是一个返回 JSON 对象数组的 API。我需要逐步检查这些对象,并为每个对象进行另一个 AJAX 调用。问题是处理每个 AJAX 调用的系统一次只能处理两个活动调用(因为它是一个与桌面应用程序挂钩的 CPU 密集型任务)。
我想知道如何使用 RxJS(使用版本 5 或 4)实现这一点?
编辑:此外,是否可以同时运行一系列步骤。即
Downloading File: 1
Processing File: 1
Converting File: 1
Uploading File: 1
Downloading File: 2
Processing File: 2
Converting File: 2
Uploading File: 2
Downloading File: 3
Processing File: 3
Converting File: 3
Uploading File: 3
我试过做类似的事情:
Rx.Observable.fromPromise(start())
.concatMap(arr => Rx.Observable.from(arr))
.concatMap(x => downloadFile(x))
.concatMap((entry) => processFile(entry))
.concatMap((entry) => convertFile(entry))
.concatMap((entry) => UploadFile(entry))
.subscribe(
data => console.log('data', new Date().getTime(), data),
error => logger.warn('err', error),
complete => logger.info('complete')
);
但这似乎不起作用。例如,downloadFile 不会等待 processFile、convertFile 和 uploadFile 全部完成,而是在前一个完成后立即再次运行下一个。
【问题讨论】:
-
@cport1 - 链接的示例一个接一个地运行,无需等待任何形式的异步操作。这和我问的不一样。
标签: javascript rxjs rxjs5