Ionic 2:HTTP 请求 "pool" 和回调
Ionic 2: HTTP requests "pool" and a callback
我有一个算法可以将缓存放入 SQLite 数据库中。我调用 API 的每个端点并将其保存在本地数据库中。我需要知道所有这些何时完成。在 angular 1 中,我们有一个 Promise 队列,它在请求池完成时调用回调。在 ionic 2 / angular 2 我怎样才能实现它?
谢谢。
您应该为此使用 rxjs
,例如(在 Typescript 中):
const apiCalls = [
http.post(apiUrl1, arguments1, ...),
http.post(apiUrl2, arguments2, ...),
http.post(apiUrl3, arguments3, ...),
...
];
Observable.merge(...apiCalls).subscribe(undefined,undefined,() => myOnComplete());
对merge
的调用将导致对运行的所有请求并发(当调用subscribe
时)。 subscribe
接受 3 个回调函数,onNext(为每个发出的项目调用)、onError(发生错误时调用)和 onComplete(当 observable 成功终止时调用)。
我有一个算法可以将缓存放入 SQLite 数据库中。我调用 API 的每个端点并将其保存在本地数据库中。我需要知道所有这些何时完成。在 angular 1 中,我们有一个 Promise 队列,它在请求池完成时调用回调。在 ionic 2 / angular 2 我怎样才能实现它?
谢谢。
您应该为此使用 rxjs
,例如(在 Typescript 中):
const apiCalls = [
http.post(apiUrl1, arguments1, ...),
http.post(apiUrl2, arguments2, ...),
http.post(apiUrl3, arguments3, ...),
...
];
Observable.merge(...apiCalls).subscribe(undefined,undefined,() => myOnComplete());
对merge
的调用将导致对运行的所有请求并发(当调用subscribe
时)。 subscribe
接受 3 个回调函数,onNext(为每个发出的项目调用)、onError(发生错误时调用)和 onComplete(当 observable 成功终止时调用)。