Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Action after push elements in array

I have implemented a function that I use to call an api, recover for each product some info and push into array. After that I have done this for all products, I would to do a action with the array.

So I have:

newProducts: any = []

 loadOfferRelated(offer) {
    // consider this offer with 2 products array.
    let products = offer.products
    for (let i = 0; i < products.length; i++) {
    let apiCall = this.offerService.apiCall(products[i].id, product.catalogId)
    apiCall.pipe(untilDestroyed(this)).subscribe(
       (data) => { operationOnArray(data, products[i])}
     )
   }
   if(this.newProducts.length > 0){
   // ---> Here i should call another function but it doesn't enter there)
  }

 operationOnArray(data, product){
   // I make some operation product and save in array
  this.newProducts.push(products)
  console.log("this.newProducts", this.newProducts) <-- this is every for populated + 1
  return
 } 

I have a problem to call the if when the array newProducts is populated, how can I do?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Ok so you can use a forkJoin, to make the api calls simultaneously and use the map operator to return each transformed element, push the returned data into the array and then finally call the if condition.

    newProducts: any = [];

    loadOfferRelated(offer) {
        // consider this offer with 2 products array.
        let products = offer.products;
        const apiCalls = products
            .map((product: any) => this.offerService.apiCall(product.id, product.catalogId))
            .pipe(
                map((data: any, i: number) => {
                    // i am not sure what your doing with the api call (data) so I am merging product and data, you customize
                    // to your requirement
                    return { ...products[i], ...data };
                })
            );
        forkJoin(apiCalls).subscribe((newProducts: Array<any>) => {
            this.newProducts = newProducts;
            if (this.newProducts.length > 0) {
                // ---> Here i should call another function but it doesn't enter there)
            }
        });
    }
about 4 years ago · Santiago Trujillo Report

0

Your problem is that whatever happens in you subscription is asynchronous. That means that your if is triggered before your request has ended, so it's most likely that your if condition will always end with a false.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!