When the boolean in RxJS skipWhile remains true

My current scenario involves a specific use-case:

There is a service that queues calculation tasks assigned with a certain ID. I have set up an observable to monitor the status of the service. When the ID is still in the queue, the query result is true (indicating that the task has not been calculated yet). Once the query result is false, it means that the task has been completed.

I am searching for a way for the observable to emit only when the returned value is false. I have attempted to achieve this using .skipWhile and .filter, but have not been successful in getting the desired outcome.

private calc = new Subject<T>();

calculate(id: number): Observable<T> {
    this.calcService.searchID(id).filter((x: boolean) => x == false)
         .subscribe((dat: boolean) => {
              this.calc.next(T);
    });
    return this.calc.share();
  }

or

calculate(id: number): Observable<T> {
    this.calcService.searchID(id).skipWhile((x: boolean) => x == true)
         .subscribe((dat: boolean) => {
              this.calc.next(T);
    });

    return this.calc.share();
  }

(Apologies for the use of == true/false)

I hope someone can provide me with some guidance. I am using RxJS 6.

Edit: Apologies for the oversight. this.calcService.searchID(id) is actually an HTTP request. And with each request, it returns a boolean value. Therefore, I may receive false after 20 requests or after 100. The timing depends on how fast the calculation is processed by the service. So, I need a way to either wait for the response to be false or retry the request until false is obtained.

Answer №1

To ensure proper monitoring of a series of service calls, it is recommended to wrap the calls in an observable. Each service call is expected to emit a single value and then complete.

For reference, you can check out this StackBlitz example: https://stackblitz.com/edit/typescript-dwmwls?file=index.ts

It is worth noting that caution should be taken when using first(predicate) as it may throw an error upon completion if no value satisfying the predicate is emitted. As an alternative, consider using filter(predicate) along with take(1).

Another approach could be replacing first with skipWhile alongside take(1), which should yield the desired outcome. In the realm of rxjs, there are often multiple methods to achieve the same result.

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

Issues with external javascript link functionality

I'm having trouble connecting my external JavaScript file to my HTML code. I'm attempting to concatenate two strings using an input function and then display the new string in the empty text field. What could be causing this issue? Appreciate an ...

Node.js post request body is still showing as undefined despite using body-parser

Hello everyone, I am currently using Node.js to implement a Dialogflow chatbot. My goal is to extract parameters from an HTTP POST request. To achieve this, I utilized Postman and made sure to set the content type to JSON in the header. Below is the code f ...

Discover identical JSON elements in 2 JSON arrays using JavaScript and add CSS styles to the corresponding items within a UL list

In order to manipulate JSON data using JavaScript, I am required to create an HTML UL list of tags based on a JSON array of Tag items. Furthermore, there is a second set of Tags in another JSON data-set that needs to be compared with the first Tag JSON da ...

What to do when Observable fails to close due to an error?

Consider the following code snippet: Observable.timer(0, 10000) .switchMap(() => this.http.get(url)) .retryWhen(errors => errors.delay(10000)) .subscribe(data => displayData(data)) This code ensures that an HTTP call is made every 10 seconds wit ...

How can a functional component be created in VueJS using x-templates?

Looking to create a functional component in VueJS with a template as a single-file component? Utilizing x-templates means your component will be stored in a .html file. Want to transform the given component into a functional component? <!-- my_compone ...

Avoiding conflicts among jquery prototype/plugin methods

Imagine I have a primary JavaScript file on my website that includes the following code: $.fn.extend({ break: function(){ // Add your custom code here }, cut: function(){ // More code here }, // ...and many other methods }); When using t ...

preventing the propagation of another event

Is it possible to listen for a change event on a checkbox without triggering a click event on its container? <label><input type="checkbox"> Hello world</label> I need the action triggered by the checkbox's change event, but I want ...

Guide on retrieving specific values within an array and mapping them to separate arrays using the HttpClient service in Angular 7+

I'm having a tough time grasping the concept of handling data once it's returned from an Http Request in Angular 7+ The Service responsible for fetching each location is shown below: import { Injectable } from '@angular/core'; import ...

What is the best way to extend the width of an element within a Bootstrap column beyond the column itself?

Apologies for any language errors, but I have a query regarding Bootstrap. I am currently working on making a website responsive and I have a row with 4 columns set up like this: The "seeMore" div is initially hidden and on clicking the boxToggle element ...

What to do when calling disabled() on a FormControlName causes all form fields to become invalid?

While working with a reactive form, I have observed that setting a formControlName to disabled() can cause the entire form to become invalid. Is there a way to ensure the form remains valid even after disabling a control? console.log('Before:' ...

How do I set up a timing mechanism in a VSCode extension to automatically clear error messages after a specified duration?

I'm currently in the process of developing a VSCode extension, and ran into an issue where I need to display an error message if a certain condition is met. However, I want this error message to automatically disappear after a set amount of time (say, ...

Restrict the Angular ng-repeat directive to specific rows only

Suppose we have a JSON dataset listing all languages of books: $scope.data = [{ "title": "Alice in wonderland", "author": "Lewis Carroll", "lang": ["en"] }, { "title": "Journey to the West", "author": "Wu Cheng'en", "lang": [" ...

Edge Runtime does not permit the use of Dynamic Code Evaluation functions such as 'eval', 'new Function', and 'WebAssembly.compile'

My project involves the implementation of NextUI components. I incorporated the Select component from NextUI, and during development mode, everything functioned flawlessly. However, upon completion of development and attempting to build the project, I enc ...

What mistakes am I making in including/injecting functions in AngularJS controllers and factories?

I'm encountering an issue in Angular where I am struggling to inject my factory into my controller. The error message I'm receiving is "Cannot read property 'validar' of undefined". In my project, I have two files - ServiceUtil.js which ...

Tips for efficiently displaying a computed property on a template using Vuetify?

I am attempting to display only the initials of a user who is logged in while inside a store. Here is my template: <v-menu v-if="this.$store.getters.getLoggedUser"> <template v-slot:activator="{ on, attrs, userInitials }&quo ...

Vue: Load page content only after the route change is complete

Every time I visit a new route within the current session, I notice that the page loads and then takes about 2 seconds for the styles to be applied. This delay causes what is commonly known as CSS flashing or FOUC (Flash of Unstyled Content), which ultimat ...

Unable to capture screenshot of hovered element using Cypress

Having an issue with taking a screenshot of an element with a hover effect. The screenshots always come out without the hover effect applied. tableListMaps.lineWithText('Hello world', 'myLine'); cy.get('@myLine').realH ...

Substitute all instances of numbers in scientific notation with standard numbers

I need help handling a large XML string containing decimal values in both regular and scientific notation. I am looking for a way to convert all exponential values to regular notation directly. So far, I have developed the following Regex pattern that ide ...

Using a render target causes certain elements of my visual graphics to become hidden

Hey there, I've been experimenting with render targets lately and encountered some issues. I've put together a simplified example below: init = function() { // RENDERER canvas = document.getElementById("mycanvas"); renderer = new THREE ...

What is the process for establishing Many-to-Many connections with personalized field titles using Bookshelf.js?

I am interested in setting up a many-to-many relationship using Bookshelf.js and I would like to customize the names for the foreign key columns. Additionally, I want to have access to the helper table in Bookshelf just like in the example below: var Phys ...