1

I have multiple promise functions as:

function test1(){
     const numberPromise = new Promise((resolve) => {
          resolve(1);
     });
     return numberPromise;
}

function test2(){
     const numberPromise = new Promise((resolve) => {
          resolve(2);
     });
     return numberPromise;
}

function test3(){
      const numberPromise = new Promise((resolve) => {
          resolve(3);
      });
      return numberPromise;
}

I want to call it parallel just like Promise.all([test1(),test2(),test3()]) but I don't want to use promise, I want to use Observable.

To get this done I am doing this:

import { Observable } from 'rxjs/Observable';
var data=Observable.of(test1(),test2(),test3());
    data.subscribe((data)=>{
            console.log("data :",data);
})

But I am getting the result like : enter image description here

But I want to get this like :

data :1
data :2
data :3



  [1]: https://i.sstatic.net/0wamC.png
1
  • Promises are executed async doesn't that make them parallel ? Commented Jun 10, 2019 at 12:29

1 Answer 1

4

Try to use forkjoin

const example = forkJoin(
  //emit 'Hello' immediately
  of('Hello'),
  //emit 'World' after 1 second
  of('World').pipe(delay(1000)),
  //emit 0 after 1 second
  interval(1000).pipe(take(1)),
  //emit 0...1 in 1 second interval
  interval(1000).pipe(take(2)),
  //promise that resolves to 'Promise Resolved' after 5 seconds
  myPromise('RESULT')
);
//output: ["Hello", "World", 0, 1, "Promise Resolved: RESULT"]
const subscribe = example.subscribe(val => console.log(val));

Example here.

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.