`The simultaneous processing of two inputs using forkJoin`

There are two input fields on my form: Street address and Zipcode. Each field emits its value on keyup as a Subject in Angular.

Before calling an API endpoint with these values, I need to ensure that both the street address and zip code values are valid.

For the street address observable:

this.searchableComponent.value
            .do((value: any) => {
                if (value.length < this.MINIMAL_LENGTH_FOR_SEARCH) {
                    this.suggestions = [];
                }
            })
            .filter(() => this._zipcodeModel !== undefined && this._zipcodeModel.city.length > 0)
            .filter((value: any) => value.length >= this.MINIMAL_LENGTH_FOR_SEARCH)
            .debounceTime(this.DEBOUNCE_TIME_IN_MS)
            .distinctUntilChanged()
            .do(() => console.log('ready street address'))

For the zip code observable:

this.zipcodeInput.value
        .filter((value: string) => {
            const pattern = new RegExp(US_ZIPCODE);

            return pattern.test(value);
        })
        .distinctUntilChanged()
        .mergeMap((zipcode: string) => this.addressService.lookup(zipcode))
        .map((zipcodeModel: ZipcodeModel) => this._zipcodeModel = zipcodeModel)
        .do(() => console.log('ready zipcode'))

Testing with a simple forkJoin (same as combineLatest attempt):

forkJoin(this.searchableComponent.value, this.zipcodeInput.value)
            .subscribe((results: any) => {
                console.log(results)
            })

I attempted to use forkJoin on both observables, but it seems that neither of them emitted anything. When I tried using combineLatest after the first observable was valid and complete, the combination of them completed on every emission of the second observable, even if it was invalid.

What is the correct approach to ensure completion of multiple observables only when all of them are completed and valid?

Answer №1

I came across an answer utilizing the method withLatestFrom.

Simply by incorporating

.withLatestFrom(this.zipcodeInput.value)
into the Street address observable, I successfully established the dependency I was aiming for.

I'm still puzzled as to why forkJoin wasn't able to achieve the desired outcome.

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

React does not accept objects as valid children. If you want to render a group of children, make sure to use an array instead

I am in the process of developing a system for document verification using ReactJS and solidity smart contract. My goal is to showcase the outcome of the get().call() method from my smart contract on the frontend, either through a popup or simply as text d ...

Determine the route parameter name based on the path string, for example, '/posts/:id'

My Route interface has a params object, and I'm looking to ensure type safety on that params object. For example: If we have a route config like this: { post: { path: 'posts/:id', } } navigate({ name: 'post', params: { wr ...

Position the circles in a way that they align along the circumference of a larger

I need help arranging a group of circles around another circle without any of them overlapping. I have all the radius measurements for each circle, as well as the coordinates for the target circle. The target circle will always be large enough to contain ...

unable to retrieve data from MongoDB using db.find

I am currently working with a script that I run through the mongo shell. The first query for posts works perfectly and I am able to receive the data object without any issues. Even when I print the date after running it through the convertDate function, ...

String defines the type

I came across the following code snippet in some external sources that I intend to incorporate into my project: const INIT: 'jsonforms/INIT' = 'jsonforms/INIT' Can someone explain what it means to define a type with a string like INIT ...

The value returned by elementRef.current?.clientHeight is not the correct height of the element

I've encountered a peculiar issue with my code where the reported height of an element does not match its actual size. The element is supposed to be 1465px tall, but it's showing up as 870px. I suspect that this discrepancy might be due to paddin ...

"How can I use node.js to retrieve the number of connections on an HTTP server

I have set up a Node.js HTTP server using the following code: http.createServer(function(req, res) {}).listen(8181); I am interested in finding a straightforward way to monitor the performance of my Node.js HTTP server from within the same process. I wou ...

Steps to turn off sourcemaps in the @sentry/nextjs package

Currently, I am working on a Next.js project and have incorporated the @sentry/nextjs package for error logging purposes. My goal is to turn off sourcemaps in the client side of my deployed application. Despite going through this informative blog post fro ...

What is the best approach to unit testing this React Component?

I have created a component that acts as a wrapper for another component. My question is, how should I approach unit testing for this component? Besides checking the state and method calls to ensure they update the state correctly. Other than rendering pro ...

Retrieving Response Status Codes with Angular 4 and Express

When making GET requests to an Express REST API, I am setting res.status to 200 and returning data. However, in Angular, the response contains the data, but response.status always returns undefined. [UPDATE START] After trying Martin Adámek's sugge ...

Considering a Servlet for managing AJAX requests?

Looking for advice on best practices for implementing a yes or no question with AJAX requests. Open to feedback if I make any missteps along the way. I have a specific Servlet (AjaxServlet?) designated to handle all AJAX requests AjaxServlet is mapped t ...

The ajax client is encountering an undefined response, but it is correctly processed when accessed through the

I am in the process of setting up an ajax client with a dummy server for testing purposes. I have successfully resolved the cors issue, but now I am facing a problem where the response from the ajax client is showing as undefined. Interestingly, when I acc ...

The JSON key has been labeled as "valid", making it difficult to access in JavaScript as demonstrated in a JSfiddle example

Initially, I transformed a Plist file (XML formatted) into JSON using an online tool. Extracting the important data from this extensive JSON file was not a challenge. Utilizing this crucial data, I am reconstructing a new JSON file that is concise and cont ...

Arranging the index of an array according to a separate array of objects in JavaScript using Vue

I have two arrays of objects that need to be sorted based on the value of a key in the other array. array1: [ { group: 'GROUP1', sort_order: 1, }, { group ...

Limitless scrolling functionality in asp.net

I attempted to implement an infinite scroll feature using a WebMethod, but I keep receiving a "failed" message. After reviewing my code, I couldn't identify any errors. Below is the code snippet: Code Behind: public int CurrentPage { get ...

"The variables in the .env file are not reflecting the latest updates. What is the best way

Currently, I am in the process of developing an application using Quasar (Vue) where I have stored my database keys in a .env file. Recently, I encountered an issue when attempting to switch to another instance and updating the keys in the env file. Despit ...

Exploring the power of Express.js by utilizing local variables and rendering dynamic views

I am in the final stages of completing an application using node.js and express, even though I am still relatively new to them. Here's my situation: In my app.js file, I have declared a variable app.locals.webLang and set its initial value to "EN". T ...

Incorporating an additional table dynamically based on height considerations – can you provide visuals as guidance?

The visual representation of my question far surpasses my verbal explanation. My intention is to insert a new table once the height of the previous table exceeds a specific HEIGHT limit, not a certain number of rows. This project does not involve creat ...

Encountering a Laravel Nova issue where attempting to override a Vue component leads to a Vue warning: Error

Recently, I decided to incorporate a user guide into my nova using the following Vue Shepherd library. To make this work, I made some adjustments in the files within the nova directory. One of these changes involved renaming the file "webpack.mix.js.dist" ...

I am experiencing some challenges with my code as the Jquery Ajax function does not seem to be functioning correctly. Can

I am trying to incorporate a for loop (or `do/while loop) in my code, but unfortunately, it is not functioning as expected. The current setup is only working for a single item, and the goal is to have multiple items on a single invoice by utilizing the lo ...