0

I'm trying to chain a few requests in a series, something like forkJoin but the requests NOT being requested in parallel. Here's what I have so far:

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)
  })

I read somewhere that concat can be used in combination with toArray, but that was apparently taken out in recent rxjs updates. Is there any way to do this currently?

EDIT - The final goal is something similar to this answer. The code in that answer is no longer working in Angular 7 and Rxjs 6.2.2.

3
  • 2
    So you can use merge or [concat] (learnrxjs.io/operators/combination/concat.html) depending on your requirement s of maintaining order (or not). Commented Dec 13, 2018 at 11:04
  • Just use concat Commented Dec 13, 2018 at 11:26
  • So I just read through the documentation for concat and merge again, and here's the issue. I need the responses in a single array - I don't want to subscribe to each response as it arrives. Commented Dec 13, 2018 at 12:08

2 Answers 2

2

Here's what ended up working:

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
    })

The toArray() operator waits for all responses - in the order provided in concat.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use concat from Rxj6. Tyry something like that:

//  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)

The use forkjoin for parallel or Rxjs operator, like concatMap for non-parallel

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.