Q:

how to use forkjoin from rxjs in NodeJs for multiple call using NPM request

I have to make multiple serialise call on server side in NodeJs using  request module , and RxJs Observable
I have array of Url Address like ['www.google.com','facebook.com']
When I make request to call then server get busy in event loop . I want to know when my last url call gets response so that I can send response to front end
here is my code so far
 const foo = Observable.create(function (observer) {
         res.writeHeader(200, { "Content-Type": "text/html" });
         var str = '<html><head></head><body><h1> Following are the responses: </h1><ul>'
         res.write(str);
         let count = 0;
         addressesArray.forEach(function (element, i) {

         observer.next(element);
         if (some Condition) {
            console.log('here i want to call complete when I get last response')     
       // observer.complete();   
         }

         })
     }) 

and this is my subscribe function 
     const subscription = foo.subscribe({
         next : addres =>
         {
             request(addres , function (error, response, body) {
                if (!error && response.statusCode == 200) {

                     console.log(response)

                 }
                if (typeof response === "undefined" || typeof body === "undefined") {
                     console.log('No resp')
                 }
            })

     },
     complete:()=>{console.log('done')}

 })

how can I use Forkjoin for this

A:

You can turn each URL into an Observable and then use forkJoin to wait until all of them resolve:
import * as request from 'request';
import { bindNodeCallback, forkJoin } from 'rxjs';

const requestAsObservable = bindNodeCallback(request.get);
const observables = addressesArray.map(url => requestAsObservable(url));

forkJoin(...observables)
  .subscribe(console.log);

You could also you request-promise package that just returns Promises and you wouldn't have to use bindNodeCallback to wrap request.

