Ensure Rxjs waits for the completion of the previous interval request before moving forward

Scenario: It is required to make an API call every 3 minutes to update the status of a specific service within the application.

Here is my existing code snippet:

interval(180000)
            .subscribe(() => this.doRequest
            .pipe(catchError(() => {
                    this.applicationFlag = false;
                    return EMPTY;
                }))
                .subscribe(result => this.applicationFlag = result));

I am currently facing an issue where the previous interval request is not completed, but the next interval request is triggered.

Is there a way to set a flag to wait for the previous request to be completed before executing the next interval request?

Answer №1

When you nest one subscribe inside another subscribe, the outer chain cannot be notified of the inner chain's completion. To address this issue, you need to restructure your chain and utilize operators like concatMap, mergeMap, or switchMap. Here's an example of how you can do it:

interval(180000)
  .pipe(
    concatMap(() => this.doRequest.pipe(
      catchError(() => {
        this.applicationFlag = false;
        return EMPTY;
      }),
    ),
  )
  .subscribe();

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

Adding the classname "active" in ReactJS can be achieved by utilizing the `className` attribute within

I am facing an issue with adding the active classname in my code. Can anyone suggest a solution to add the active classname for this section: <li onClick = {() => onChangeStatus({status: 'on-hold'})} className = {appState === {'status& ...

Guide on linking an Angular2+ app with an external API

Can anyone provide guidance on how to integrate an external API with authentication (username and password) into an Angular Application? I am comfortable connecting to APIs that don't require authentication, but I am facing difficulties with APIs that ...

What is the best way to display various components based on the user's device type, whether it be web

How can I use Angular 7, TypeScript, bootstrap, ngx-bootstrap, etc., to switch between components based on the user's device (desktop vs mobile)? I have noticed that many websites display different components when resized. I wonder if these are simpl ...

Best Placement for Socket.io Server in WebStorm Express Template

Trying to integrate socket.io into an express server generated by WebStorm. Should the setup of the server and socket.on events all be placed inside /bin/www, or is it better practice to create separate controllers like index and users pages? https://i.ss ...

Scrolling to the next div will automatically align it in the center of the screen for the user

I am in the process of creating a single-page website and I would like to stack multiple divs on top of each other, all approximately 400px in size (with variations). Instead of using a smooth scroll, I want the page to jump to the next div and have it cen ...

Detecting repeated keys in a query for a REST connector in loopback

Can anyone help me figure out how to duplicate parameters in a loopback REST connector query? Here's the code snippet I'm working with: details: { 'template': { 'method': 'GET', 'debug': tr ...

Unable to choose Typescript as a programming language on the VSCode platform

Recently, I encountered an issue while using Visual Studio Code with TypeScript. Even though TypeScript is installed globally, it is not showing up in the list of file languages for syntax highlighting. Despite trying various troubleshooting methods such a ...

Encountered a mistake while trying to deploy an Angular 2 application on Heroku - received an error message indicating an expected expression but instead got

I am encountering an issue with my Angular 2 app. It runs perfectly fine locally, but when I deploy it on Heroku, I'm getting the following errors: >SyntaxError: expected expression, got '<' ><!DOCTYPE html> >shim.min.js ( ...

The iron-session package does not export the path ./next/dist from the package

I am encountering an issue while using iron-session. Below is the code snippet causing the error: import { withIronSessionSsr } from 'iron-session/next/dist'; ... export const getServerSideProps = withIronSessionSsr(async function ({ req, r ...

The automated Login Pop Up button appears on its own and does not immediately redirect to the login form

Hey guys, I'm struggling with modifying the jquery and html to ensure that when the login button is clicked, the login form pops up instead of displaying another login button. Another issue I am facing is that the login button seems to pop up automati ...

Generate a new perspective by incorporating two distinct arrays

I have two arrays containing class information. The first array includes classId and className: classes = [ {classid : 1 , classname:"class1"},{classid : 2 , classname:"class2"},{classid : 3 , classname:"class3"}] The secon ...

What is the best way to get both the id and name of a selected item in Angular?

Within my select field, data is dynamically populated based on names. My objective is to not only capture the selected name but also its corresponding ID. Here's a snippet of my HTML template: <select class="custom-select d-block w-100" id="test" ...

Reactive form within a parent object for nested counting

I am looking to generate a nested form based on the following data: The current data available is as follows: mainObject = { adminname: 'Saqib', adminemail: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="40 ...

The Ionic search bar will only initiate a search once the keyboard is no longer in view

In my Ionic application, I have implemented a search bar to filter and search through a list. The filtering process is triggered as soon as I start typing in the search bar. However, the updated results are not displayed on the screen until I manually hide ...

Reimagine server-side storage options as an alternative to remixing JavaScript local storage

My remix application is designed to serve as a frontend. I retrieve data from the backend and sometimes need to load specific data only once and reuse it across multiple pages. In our previous frontend setup, we utilized localstorage; however, with the cur ...

Converting camera space coordinates to scene space in three.js

I want to position a cube relative to the camera instead of relative to the scene. However, in order to display it in the scene, I need to determine the scene coordinates that align with the cube's camera space coordinates. I came across the function ...

Acquiring the selector value from a tag

To summarize: This snippet of code: for(let i = 0; i <= items.length; i++){ console.log(items[i]) } Produces the following output: <a class="photo ajax2" target="_blank" href="/profile/show/3209135.html" data-first="1" data-next="3206884"> ...

Is there a way for one function to access the validation of a nullable field performed by another function?

Below is a TypeScript code snippet. The function isDataAvailable will return true if the variable data is not undefined. However, an error occurs in the updateData function when trying to access data.i++: 'data' is possibly 'undefined'. ...

How to showcase MongoDB data directly on the homepage

Looking for advice on how to display MongoDB database records on the front end of my website using Express. Each record has a category, and I want to organize and display them accordingly. As a beginner, any tips or suggestions would be greatly appreciat ...

Vue - Unable to navigate to a different route

I just started working with Vue and attempted to redirect '/home' to '/travel', but for some reason it's not functioning correctly. Can someone please guide me on how to achieve this? What could be the issue with my code? Thank y ...