When all observables have finished, CombineLatest will only execute one time

I've encountered a problem that requires me to use a combination of 3 route observables to open a modal. Here is the logic in my code:

combineLatest([obs1, obs2, obs3])
    .subscribe(([s1, s2, s3]: any) => { 
        openModal();
    });

The issue I'm facing is that since this code gets executed 3 times, the modal opens three times as well, which is not the desired behavior.

To handle this, I resorted to a less than ideal solution by introducing a flag like so:

let shouldOpen = true;

combineLatest([obs1, obs2, obs3])
    .subscribe(([s1, s2, s3]: any) => { 
        if(shouldOpen) {
            shouldOpen = false;
            openModal();
        }
    });

However, I am aware that this workaround is not optimal.

My question now is whether there is a way for me to continue using combineLatest but ensure that it is only executed once?

You can also check out this Stackblitz link to experiment with combineLatest.

Note: I have already tried and cannot use forkJoin or zip for this scenario.

Answer №1

When forkJoin is not an option (even though it's the right choice), you can choose to ignore the first two emissions from the combineLatest stream.

combineLatest([obs1, obs2, obs3])
  .pipe(skip(2))
  .subscribe(([s1, s2, s3]: any) => { 
    openModal();
  });

If you prefer, you could utilize skipWhile to wait until all three values are defined:

      .pipe(skipWhile(([s1, s2, s3]) => s1===undefined || s2 === undefined || s3===undefined))

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

Guide to sending a text from HTML to an Angular 8 Component through the use of the (click) function

After successfully implementing this code: <a [routerLink]="['/menuItemOne']"> <span [innerHTML]="(mystrings$ | async)?.menuItemOne | htmlToText"></span> </a> I now face the challenge of updating t ...

How come I am unable to upload my local image using angular-cli?

When testing my application on localhost, everything runs smoothly. However, once I publish the app to GitHub, the image fails to load. The image is located within the src/resources directory, and I have also attempted moving it to the public folder. My ...

filter failing to provide output

Issue with fetching partnername from the filter function, always returning undefined. administrationList = [ { "runid": 6, "partnerid": 2, "partnername": "test admin2", }, { "runid& ...

Utilize async/await to send images using node mailer

How can I correctly access mailOptions in the triggerExample.ts file? mail.ts: export const sendNewMail = async (html: string, emails: string[]) => { let smtpTransport = nodemailer.createTransport({ service: "Gmail", auth: { u ...

The Highcharts Angular library is struggling to access the 'series' property due to it being undefined

Encountered an error message that has puzzled me. I am eager to explore highstock.js and its associated files, but I'm uncertain about the best approach. Despite the chart displaying correctly, this error consistently arises. https://i.sstatic.net/p ...

What is the best way to add an array object's property value to HTML?

I am attempting to build a basic carousel using DOM manipulation. I am not sure if it can be done in Angular, but my idea involves creating an array of objects with specific properties that I want to pass to the HTML through interpolation. However, I am ...

Encountering some problems while trying to use {return this.getAll.get(this.url)} to consume an API within Angular

I am currently delving into the world of Angular and Typescript to interact with APIs. While I have some experience working with APIs in Javascript, I am relatively new to this setup. The issue I am facing revolves around the code snippet below: To tackle ...

Deactivate the selection option in Syncfusion NumericTextbox

I have integrated an Angular NumericTextbox component from Syncfusion into my application. A problem we encountered is that when the input is clicked, it automatically gets selected. Is there a way to disable this behavior? Problem: https://gyazo.com/a72b ...

The type 'string' cannot be assigned to type 'ImageSourcePropType'

Context Attempting to utilize a SVG component in React Native with the xlinkHref property within an Image tag. The SVG file was converted into a React Native Component (TSX) using the tool provided at . While simple SVG icons have been successfully used be ...

The app.component.ts does not always trigger this function

I am looking to ensure that the following function is triggered every time a user visits my website, regardless of the URL, in order to track traffic. The site is built in Angular2 (v7). I have placed the function early on in the Angular execution stack ...

The parameter 'users | undefined' is not compatible with the argument type 'users'. Please note that 'undefined' cannot be assigned to type 'users'

Can anyone please help me with creating a put method using TYPEORM? I am facing an error that requests a typeof, but my users have a value. (I am a newbie, so not sure). Any guidance on how to fix this issue would be greatly appreciated. Here is the snip ...

Error in TypeScript React: "Could not locate module: Unable to locate 'styled-components'"

Even though I have installed the @types/styled-components package and compiled my Typescript React app, I am consistently encountering this error: Module not found: Can't resolve 'styled-components' I have double-checked that 'style ...

Angular 2 - utilizing dependency injection to access services from within other services

I have developed two Angular 2 services. One of them is a custom Http class with unique functionality: ServerComms.ts import {Injectable} from 'angular2/core'; import {Http} from 'angular2/http'; @Injectable() export class ServerComm ...

Circular structure error occurred when attempting to convert an object to JSON, starting at an object constructed with the constructor 'Object'

I am facing an issue where I need to update a Medico from the collection, and I have successfully destructured the data of the Medico's name and email. Additionally, I have obtained the ID of the assigned hospital. However, I am having trouble sendin ...

Encountering a 404 error when utilizing ngx-monaco-editor within an Angular application

I have been encountering an issue while attempting to utilize the editor within my Angular 8 application. Despite researching similar errors on Stack Overflow and GitHub discussions, I haven't found a solution yet. Here's how my angular.json asse ...

What is the TypeScript counterpart of ParentClass.this?

Given the Java code snippet above, how would you achieve the same functionality in TypeScript as ParentClass.this.a? class ParentClass{ a: number = 1; class ChildrenClass{ b: number = 2; run(){ this.b = parentInstance.a; // Assuming &apo ...

Is it possible to define TypeScript interfaces in a separate file and utilize them without the need for importing?

Currently, I find myself either declaring interfaces directly where I use them or importing them like import {ISomeInterface} from './somePlace'. Is there a way to centralize interface declarations in files like something.interface.ts and use the ...

What is causing the dysfunction of angular2 form when used with the HTML <form> tag?

Can someone explain the strange behavior of the <form> element in ng2 to me? I have noticed something odd and need some clarification :) To demonstrate, I have created a simple Plunker example at this link: https://plnkr.co/edit/vdrHJBNdd26y6YhPXTHD ...

efficiently managing errors in a Nest Jest microservice with RabbitMQ

https://i.sstatic.net/sUGm1.png There seems to be an issue with this microservice, If I throw an exception in the users Service, it should be returned back to the gateway and then to the client However, this is not happening! The client only sees the de ...

Choosing a specific option from a dropdown menu in Angular

Whenever I try to update the province of a person using the dropdown list, it doesn't display the old province value. This issue seems to be related to the HTML rendering. This dropdown list is a crucial part of the person's information that I f ...