Hold off on proceeding until the subscription loop has finished

Is there a way to verify that all results have been successfully pushed to nodesToExpand after running the for loop?

The getFilterResult function is being called via an HTTP request in the nodeService service.

  for(var step in paths)
  {
    this.nodeService.getFilterResult(paths[step]).subscribe(
      (node) =>
      {
        nodesToExpand.push(node);
      }
    );
  }

Answer №1

It seems like the way you are currently approaching this is not quite right. I recommend avoiding mixing RxJS with for loops. Typically, RxJS has its own features to meet your requirements, so check out for more information.

If you need to wait for multiple streams to emit an event before proceeding, consider using combineLatest(). This operator not only indicates when all HTTP requests have completed, but also provides the data in a tuple format for easy handling.

const httpCalls:Array<Observable<any>> = paths.map(step => this.nodeService.getFilterResult(step));
combineLatest(HttpCalls)
.subscribe(results => console.log(results));
// --> [Call1Result, Call2Result, ...]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The ngmodel variable is not responding to dynamic changes

I'm currently working on dynamically changing a date and getting it to reflect in the view, but for some reason it's not showing up. When the date is hard-coded in an array like this, it works perfectly fine and shows up in the view. My date : Ar ...

Getting the id of a single object in a MatTable

I'm currently working on an angular 8 application where I've implemented angular material with MatTableDatasource. My goal is to retrieve the id from the selected object in my list: 0: {id: "e38e3a37-eda5-4010-d656-08d81c0f3353", family ...

Initial hiccup with HTTP get/post request

Recently, I've encountered an issue while attempting to make HTTP get/post requests in Angular 4. Even after a fresh installation using the CLI, I've come across an unexpected problem. Upon sending a get request for the first time, console.log() ...

Image two-way binding in Angular is not performing as anticipated

My Angular component is designed to display a base64 image string received from a websocket in the HTML using an image tag. I am polling the websocket every second and attempting to display the updated image on the page. Here is my code: HTML of the compo ...

The parent DIV has a height of 0px, yet the child element is still visible

As I explored options for creating a navbar drop down menu with an accordion effect, I stumbled upon this informative YouTube video. The tutorial demonstrated the use of a custom component and manipulation of the max-height property of the card-content ele ...

Creating a backup link for a video player component in NextJs

My aim is to make sure that two video player components in a NextJS application can still access and play videos even when the application is running locally using npm run dev without an internet connection. Currently, these two components.. <HoverVi ...

The service value remains unchanged following a click event

I'm using a service to share values between 2 components. I am displaying this value in one component and trying to change it in the other by clicking on a button. However, the value is not being modified as expected. It seems like I may have some tro ...

The value of a checkbox in Ionic 2

I have implemented the code for reading checkbox values in Ionic 2 following the answer provided. However, I encountered an error message in the console: Cannot read property 'indexOf' of undefined Here is my home.html code: <form #leadsF ...

What is the proper way to invoke a child method after converting an object from a parent class to a child class?

When we have a subclass B that overrides a method from its superclass A in TypeScript, why does calling the method on an instance of A result in the parent class's implementation being called? In TypeScript, consider a class called Drug with properti ...

JS- Catching Errors Inside and Outside

Imagine having 2 catch blocks in your code: try { axios.get('https://example.com', {}) .then(function (responseOne: any) { return axios.post('https://example.com', {}, {}) }).then(async function (responseTwo: any) { return a ...

Most Effective Approach for Handling Multiple Fetch Requests Concurrently using Async-Await in TypeScript?

I am currently exploring the idea of making multiple API calls simultaneously by utilizing multiple fetch requests within an await Promise.all block, as shown below: const responseData = await Promise.all([ fetch( DASHBOARDS_API + "getGoal ...

Clicked but nothing happened - what's wrong with the function?

For my project, I have incorporated tabs from Angular Material. You can find more information about these tabs here. Below is the code snippet I am using: <mat-tab-group animationDuration="0ms" > <mat-tab></mat-tab> < ...

Guide on incorporating Bootstrap 4 beta.2 into an Angular 4 project

After attempting to install Bootstrap 4 version beta.2 along with its dependencies (jquery and popper.js), I encountered a strange problem. A SyntaxError message kept appearing in the console, specifically stating "SyntaxError: export declarations may only ...

The karma test encounters difficulties in establishing a connection with the 'chrome' instance that has been launched

Currently, I am facing an issue with my Karma test running on a nodejs jenkins pod. npm is timing out when trying to connect to the Chrome instance on the selenium hub. Everything was working fine until yesterday without any changes made to the configura ...

Share the component with css and font via npm

I am looking to release an angular 2 component on NPM. This particular component utilizes CSS that references certain fonts. I have figured out how to publish the .ts files, but I am unsure about how to handle the CSS and font files. Here is what I curren ...

Using Typescript in the browser with Babel and Webpack: Step-by-Step Guide

I've been exploring the use of Typescript in my browser with a specific architecture: Typescript in browser architecture However, I'm facing an issue with the import/export functionality when running this command: tsc && babel build-ts -d lib && ...

Angular 8 encountering issues with incomplete/impartial JSON parsing

Upon receiving a JSON object through a Socketio emit, the data appears as follows: { "single":[ { "name":"Xavi555", "data":[ { "_id":"5e2ea609f8c83e5435ebb6e5", "id":"test1", "author":"someone", ...

Rule in ESLint mandating return type for arrow functions

I currently have the following arrow function within my Angular project: this.httpClient.get('url').subscribe((response)=>{ }); It is important to note that ESLint should detect an error in the above code due to not specifying a return type. ...

React fails to acknowledge union types

I have the following types defined: export enum LayersItemOptionsEnum { OPERATOR, HEADER, } type sharedTypes = { children: string | ReactElement; }; type LayersItemStatic = sharedTypes & { label: string; option: LayersItemOptionsEnum; }; t ...

Angular 6 TypeScript allows for efficient comparison and updating of keys within arrays of objects. By leveraging this feature

arrayOne: [ { id: 1, compId: 11, active: false, }, { id: 2, compId: 22, active: false, }, { id: 3, compId: 33, active: false, }, ] arrayTwo: [ { id: 1, compId: 11, active: true, }, { id: 2, compId: 33, active: false, ...