Set an action after a promise?

Hi, I currently have code that allows people to get locations from a list of agents in a database and then check their distance from a users input location using a Google API.

My problem is I want the page to then go to a new results page after it has run through the database and determined the closest agent. No matter where I place

wixLocation.to("/search2"); 

It always goes to the page before running through the database. Am I supposed to create a delay, or is there a way to determine when it has completed searching the database?

Here is my current code:

import wixWindow from 'wix-window';
import wixData from 'wix-data';
import wixLocation from 'wix-location';

import {local} from 'wix-storage';
import {session} from 'wix-storage';
import {agentsearch} from 'backend/gapi';

$w.onReady(function () {
});

export function vectorImage1_click(event, $w) {
	
	  $w("#dealerDataset").onReady( () => {
	  	//Get the size of the database
		let count = $w("#dealerDataset").getTotalCount();
		console.log("Size of the Dataset: ", count);
		
		$w("#dealerDataset").getItems(1,count)
		  .then( (result) => {
		  	return searchclosest(result, count);
		  } ) // End of getItems
		  .catch( (err) => {
		    let errMsg = err.message;
		    let errCode = err.code;
		    console.log("Error Message: ", errMsg);
		    console.log("Error Code: ", errCode);		    
		  } ); // End of catch
	  }); //End onReady()
	
 //wixLocation.to("/search2"); //Doesn't Work here
 
} //End function

export function searchclosest(result, count){
  for(var i = 0; i < count; i++){
   findDistance(result.items[i].address, $w("#input1").value);
  } //End of for loop
  
 //wixLocation.to("/search2"); //Doesn't Work here  
}

export function findDistance(origin, destination){
	//Call backend function agentsearch()
	//Used to determine the distance between 2 locations
	agentsearch(origin, destination).then(function(resp) {
	let dist = resp.rows[0].elements[0].distance.value;
	let currentbest = session.getItem("distance");
	
	if( dist < currentbest || currentbest === "" ) {
		console.log("Yes, ", dist, " is closer!");
		
		session.setItem("location", origin);
		session.setItem("distance", JSON.stringify(dist));
	}
 }); // end of then
}

// Just resets the session values
export function input1_keyPress(event, $w) {
	session.setItem("location", "No Origin");
	session.setItem("distance", "");
}

Came up with a solution, just using

	wixWindow.openLightbox("Loading Lightbox");	  
	setInterval(function(){
			wixLocation.to("/search2");
			}, 10000); 

where it opens up a lightbox to show that it is loading and set it to change pages after 10 seconds. Just so that everything happens before it does change.

Hi websitejazz.

You can call wixLocation.to(“/search2”) just after searchclosest call.

$w("#dealerDataset").getItems(1,count) 
   .then( (result) => { 
      searchclosest(result, count); 
      wixLocation.to("/search2");
   } ) // End of getItems

Regards,
Genry.