Order of Code Execution?

Obviously I don’t understand the order code is executed, so I’m hoping someone can explain what’s going on. I have this code below. The problem is, if there IS a match (i.e. maincomponent == answer) I want the FIRST redirection to execute, but unless I comment out the SECOND redirection, the SECOND one always executes. My code incorrectly assumes that the FOR LOOP will fully execute before the SECOND redirection is executed. Any advice would be greatly appreciated - thanks!

                                    wixData . query ( "Bicycles" ) 
                                        . eq ( "part" ,  components . items [ 0 ]. part ) 
                                        . find () 
                                        . then (( bicycles ) => { 
                                            **if**  ( bicycles . items . length  >  0 ) { 
                                                
                                                **for**  ( **let**  i  =  0 ;  i  < ( bicycles . items . length  +  1 );  i ++) {                                                         

                                                    **if** ( bicycles . items [ i ]. maincomponent  ==  answer ) { 

                                                        wixLocation . to ( '/parts?Part='  +  bicycles . items [ i ]. maincomponent ); 
                                                        **break** ;    
                                                    } 

                                                    wixLocation . to ( '/parts' ); 
                                                } 
                                            } 
                                        })

Yes, the for loop runs first.
I think it’ll be more readable (and therefore easier to control, if instead of explicit looping, you’ll use the array.filter method. Something like:

//...
.then(bicycles => {
const items = bicycles.items;
if(items.length){
const hasMatch = items.filter(e => e.maincomponent === answer).length;//check if there's a match to the answer.
wixLocation.to(hasMatch ? '/parts?Part=' + answer : '/parts');
} else {
//...if there're no items at all do whatever you want
}
})

Note:
If you continue with your loop, it should be bicycles.items.length - 1

Hi J.D., thanks very much for answering, really appreciate that. But my problem is that the FOR loop doesn’t complete first. (oh, and don’t worry about the " + 1" in bicycles . items . length + 1 since that was just me doing a work-around to fix the issue and I forgot to put it back right when I made the post. I don’t thin I explained it well initially, here’s the thing. I want the FOR loop to have ALL the iterations you would expect if the IF condition in the FOR loop is true for any given iteration for the re-direction to happen. The problem is that the re-direction in the FOR loop only happens if I comment out the later re-direction, otherwise the later one happens. In other words the FOR loop doesn’t complete if the later re-direction is there, which seems to indicate that the code execution order isn’t what I would expect? Thanks again!

You can try adding a return in the for loop:
return wixLocation.to(‘/parts?Part=’ + bicycles.items[i].maincomponent); Maybe it will solve the issue.
Or alternatively do something like:

let url = '/parts';
 for (let i = 0; i < bicycles.items.length - 1; i++){                                                        
   if(bicycles.items[i].maincomponent === answer) {
url += '?Part=' + bicycles.items[i].maincomponent;
 break;   
  }
wixLocation.to(url);
// (btw, it's better using strict mode with triple ===)

But why wouldn’t you try my code above? It should do the work.

P.S. I think the wixLocation.to is a Promise, therefore it might starts the second one before the first one got fully fulfilled. My suggestions above deal with this option.

J.D. Thank you very much, your help is really appreciated! I actually did implement your original filter suggestion, because it’s far more elegant, but I just didn’t understand what was going on with the execution of the other code. Everything seems to be working now, and it wouldn’t have I don’t think without your help :grin: