1

I'm new to rxjs, and I need to call 3 async requests, which started out as promises, and put the reurned objects into an array in the order in which they were called. Here's what a I have (partly based on this question):

let observeFront = Observable.fromPromise(this.requestAsync(frontSlice, "front"));
let observeMiddle = Observable.fromPromise(this.requestAsync(midSlice, "middle"));
let observeEnd = Observable.fromPromise(this.requestAsync(endSlice, "end"));

let observeCombined = Observable.concat(observeFront, observeMiddle, observeEnd);

observeCombined.subscribe(data => {
  // this logs only one object - need all 3 in proper order
  console.log(data);
  this.resultsEmitter.emit(data)
});

observeFront.subscribe(response => {
  // logs the correct object
  console.log("front result:", response);
});

observeMiddle.subscribe(response => {
  // correct
  console.log("middle result:", response);
});

observeEnd.subscribe(response => {
  // correct
  console.log("end result:", response);
});

How can I emit the result only when I have [frontOb, middleObj, endObj] ?

1

1 Answer 1

1

As @Eric suggested you can use forkJoin() instead of concat():

let observeFront = Observable.fromPromise(this.requestAsync(frontSlice, "front"));
let observeMiddle = Observable.fromPromise(this.requestAsync(midSlice, "middle"));
let observeEnd = Observable.fromPromise(this.requestAsync(endSlice, "end"));

let observeJoined = Observable.forkJoin(observeFront, observeMiddle, observeEnd);

observeJoined.subscribe(data => {
  console.log(data); // => [frontOb, middleObj, endObj]
  this.resultsEmitter.emit(data)
});
Sign up to request clarification or add additional context in comments.

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.