串联合并请求 - Angular 7
Combine requests in series - Angular 7
我正在尝试将几个请求链接成一个系列,类似于 forkJoin
但这些请求不是并行请求的。这是我目前所拥有的:
let nodeDetails = this.http.get('node/1/')
let nodeParents = this.http.get('nodeParents/1/')
let nodeTree = this.http.get('nodeTree/1/')
let nodeUsers = this.http.get('nodeUsers/1/')
let nodeDocuments = this.http.get('nodeDocuments/1/')
var requests = [nodeDetails, nodeParents, nodeTree, nodeUsers, nodeDocuments]
forkJoin(requests)
.subscribe(responses => {
// List of all responses from all of the requests
console.log(responses)
})
我在某处读到 concat
可以与 toArray
结合使用,但这显然在最近的 rxjs 更新中被删除了。目前有什么办法可以做到这一点吗?
编辑 - 最终目标类似于。该答案中的代码不再适用于 Angular 7 和 Rxjs 6.2.2。
您可以使用 Rxj6 中的 concat
。试试这样的东西:
// RxJS v6+
import {concat} from 'rxjs';
let nodeDetails = this.http.get('node/1/')
let nodeParents = this.http.get('nodeParents/1/')
let nodeTree = this.http.get('nodeTree/1/')
let nodeUsers = this.http.get('nodeUsers/1/')
let nodeDocuments = this.http.get('nodeDocuments/1/')
const requests = concat(nodeDetails, nodeParents, nodeTree, nodeUsers, nodeDocuments)
使用 forkjoin
用于并行或 Rxjs 运算符,如 concatMap
用于非并行
这是最终起作用的:
import { toArray } from 'rxjs/operators';
import { concat } from 'rxjs';
let nodeDetails = this.http.get('node/1/')
let nodeParents = this.http.get('nodeParents/1/')
let nodeTree = this.http.get('nodeTree/1/')
let nodeUsers = this.http.get('nodeUsers/1/')
let nodeDocuments = this.http.get('nodeDocuments/1/')
const requests = concat(nodeDetails, nodeParents, nodeTree, nodeUsers, nodeDocuments)
requests
.pipe(toArray())
.subscribe(responses => {
// Array of responses
})
toArray()
运算符等待所有响应 - 按照 concat
中提供的顺序。
我正在尝试将几个请求链接成一个系列,类似于 forkJoin
但这些请求不是并行请求的。这是我目前所拥有的:
let nodeDetails = this.http.get('node/1/')
let nodeParents = this.http.get('nodeParents/1/')
let nodeTree = this.http.get('nodeTree/1/')
let nodeUsers = this.http.get('nodeUsers/1/')
let nodeDocuments = this.http.get('nodeDocuments/1/')
var requests = [nodeDetails, nodeParents, nodeTree, nodeUsers, nodeDocuments]
forkJoin(requests)
.subscribe(responses => {
// List of all responses from all of the requests
console.log(responses)
})
我在某处读到 concat
可以与 toArray
结合使用,但这显然在最近的 rxjs 更新中被删除了。目前有什么办法可以做到这一点吗?
编辑 - 最终目标类似于
您可以使用 Rxj6 中的 concat
。试试这样的东西:
// RxJS v6+
import {concat} from 'rxjs';
let nodeDetails = this.http.get('node/1/')
let nodeParents = this.http.get('nodeParents/1/')
let nodeTree = this.http.get('nodeTree/1/')
let nodeUsers = this.http.get('nodeUsers/1/')
let nodeDocuments = this.http.get('nodeDocuments/1/')
const requests = concat(nodeDetails, nodeParents, nodeTree, nodeUsers, nodeDocuments)
使用 forkjoin
用于并行或 Rxjs 运算符,如 concatMap
用于非并行
这是最终起作用的:
import { toArray } from 'rxjs/operators';
import { concat } from 'rxjs';
let nodeDetails = this.http.get('node/1/')
let nodeParents = this.http.get('nodeParents/1/')
let nodeTree = this.http.get('nodeTree/1/')
let nodeUsers = this.http.get('nodeUsers/1/')
let nodeDocuments = this.http.get('nodeDocuments/1/')
const requests = concat(nodeDetails, nodeParents, nodeTree, nodeUsers, nodeDocuments)
requests
.pipe(toArray())
.subscribe(responses => {
// Array of responses
})
toArray()
运算符等待所有响应 - 按照 concat
中提供的顺序。