Observable.forkJoin with a for loop

前端 未结 2 968
无人及你
无人及你 2020-12-03 22:47

I am trying to populate an array in my component called processes which is an array of process. Each process also has a list of

相关标签:
2条回答
  • 2020-12-03 23:03

    Using the for loop to make multiple HTTP requests, and then subscribe to all of them separately should be avoided in order not to have many Observable connections opened.

    As @Juan Mendes mentioned, Observable.forkJoin will return an array of tasks that match the index of each process in your processes array. You can also assign tasks to each process as they arrive as follows:

    getTasksForEachProcess(): Observable<any> {
    
        let tasksObservables = this.processes.map(process, processIdx) => {
            return myService.getTasks(process)
                .map(tasks => {
                    this.processes[processIdx].tasks = tasks; // assign tasks to each process as they arrive
                    return tasks;
                 })
                .catch((error: any) => {
                    console.error('Error loading tasks for process: ' + process, 'Error: ', error);
                    return Observable.of(null); // In case error occurs, we need to return Observable, so the stream can continue
                });
        });
    
        return Observable.forkJoin(tasksObservables);
    };
    
    this.getTasksForEachProcess().subscribe(
        tasksArray => {
            console.log(tasksArray); // [[Task], [Task], [Task]];
            // In case error occurred e.g. for the process at position 1,
            // Output will be: [[Task], null, [Task]];
    
            // If you want to assign tasks to each process after all calls are finished:
            tasksArray.forEach((tasks, i) => this.processes[i].tasks = tasksArray[i]);
        }
    );
    

    Please also take a look at this post: Send multiple asynchronous HTTP GET requests

    0 讨论(0)
  • 2020-12-03 23:15

    Thanks to Seid Mehmedovic for great explanation but it looks like code missing one round bracket near map. For me it worked as follows:

    getTasksForEachProcess(): Observable<any> {
    
        let tasksObservables = this.processes.map((process, processIdx) => {
            return myService.getTasks(process)
                .map(tasks => {
                    this.processes[processIdx].tasks = tasks; // assign tasks to each process as they arrive
                    return tasks;
                 })
                .catch((error: any) => {
                    console.error('Error loading tasks for process: ' + process, 'Error: ', error);
                    return Observable.of(null); // In case error occurs, we need to return Observable, so the stream can continue
                });
        });
    
        return Observable.forkJoin(tasksObservables);
    };
    
    this.getTasksForEachProcess().subscribe(
        tasksArray => {
            console.log(tasksArray); // [[Task], [Task], [Task]];
            // In case error occurred e.g. for the process at position 1,
            // Output will be: [[Task], null, [Task]];
    
            // If you want to assign tasks to each process after all calls are finished:
            tasksArray.forEach((tasks, i) => this.processes[i].tasks = tasksArray[i]);
        }
    );
    
    0 讨论(0)
提交回复
热议问题