Calling function from another function

Hello ,
I am trying to call a function from another function, passing a parameter and to display values from both functions on the web page but it doesn’t work.
On button click I only get value of first function displayed and when button is clicked again - second function result is also displayed.
Here is the code:
var customerID;
export function button1_click_1(event, $w) {
var userinput = $w(“#input1”).value;
getone(userinput)
.then(response => {
($w(“#text4”).text = response[0].toString());
(customerID = response[1]);
gettwo(customerID);
});

gettwo(customerID)
.then(response => ($w(“#text2”).text = response.toString()));
}

Will be very grateful if someone could help with this problem .
Regards,
Vasil

Hi, Vasil,

you have, what is called as race condition.

In your case, the output of gettwo(…) depends on passing customerID to it. But you only get customerID, once the result of getone(…) arrives.

The result of getone() will almost certainly arrive after gettwo() is already called. There is also gettwo() inside of the promise handler .then (…) of getone, but it does not populate the value to #text2. So, you are close to your solution.

Move the .then(…) statement from the outer gettwo(customerID) to one that is inside of .then(…) on getone(userinput) and you are good to go. You can delete the second gettwo(customerID).

The solution would look something like this:

var customerID;
export function button1_click_1(event, $w) {
 var userinput = $w("#input1").value; 
 getone(userinput)
   .then(response => {
		$w("#text4").text = response[0].toString();
		customerID = response[1];
		gettwo(customerID);
			.then(response => ($w("#text2").text = response.toString()));
	});
   	
}

Furthermore, you don’t really need that global customerID, so you could even optimise to this:

export function button1_click_1(event, $w) {
	var userinput = $w("#input1").value; 
	getone(userinput)
		.then(response => {
			$w("#text4").text = response[0].toString();
			gettwo(response[1]);
				.then(response => ($w("#text2").text = response.toString()));
		});
}

I hope this helps and have fun Wix Coding!