Advanced flow in function returns object promise not result

Hey there coders!
I have a tricky question and I hope som eadvanced javascript guru can read this.

I have one backend module called calculationService.jsw which I import into my script.

Inside that backend module I also import functions from dataService.jsw where I have all my data collection functions.

Now to the tricky part.

In my page I write this

let systemsizeactual = await system_size_actual(userData.user_monthlybill,regionData.region._id).toString()
	console.log("systemsizeactual:" + systemsizeactual);

And inside that function in the calculationService.jsw I console log everything and it all looks perfect.

BUT!

The console.log functions inside that function is logging results after the value has been returned. So on my page I get back [object promise] as a value.

It seem that the function or call does not wait for all the jobs to be done before it returns object promise to me.

I use await and that should normally do it, right?

The function system_size_actual is below.

export async function system_size_actual(bill, regionId) {
    // Calculation
    // [(bill * 12)/electric_price]/[irradiance]
    //
    // Conditions
    // -if bill > $200, multiply bill by .75
    //
    // Note
    // Multiply Bill by 12. Divide result by Electric Price. Divide that result by Irradiance.
  	let result;
    let electric_price = Number(await getElectricPriceByRegion(regionId));
    let irradiance = Number(await getIrradianceByRegion(regionId));
    
    if (bill > 200) {
    	bill = bill * 0.75;
    }
    result = bill * 12;
    
    console.log("RESULT: " + result);
    console.log("ELECTRIC PRICE: " + electric_price);
    console.log("IRRADIANCE: " + irradiance);
    
    result = result / electric_price;
    console.log("RESULT: " + result);
    result = result / irradiance;
    console.log("RESULT: " + result);
    result =  await getTwoDecimals(result);
    return result;
}

All the output in the functions console.log is perfect with the correct values but when calling it from my page I just get the object promise as value.

Please, someone… I am stuck with only this thing, everything else works like a charm.

Odd for sure. I’d try tossing all the content in the system_size_actual function inside of another function then using another async await to return that. Seems unnecessary but it might do it.

1 Like

I have written like 500 lines of functions this way and just thought it should work…

Andres, so my solution was a semi guess, because frankly I am not sure the difference between .jsw and .js file myself. I decided to do a little research and I think I stumbled across the real answer why your code didn’t work.

See Calling A Functiom In A Web Module Velo Web Modules: Calling Backend Code from the Frontend | Help Center | Wix.com. So the solution to toss it in another function is correct, but now we know why.

Selfishly, I have a question for you, this article alludes to it, but do you have any additional insights as to why and when we should use js versus .jsw file? I’ve written tons of code client side and am going to need to revise it, but I want to make sure I know the proper way moving forward.

1 Like

Hi Andreas.

In your example, the toString() is probably invoked on the Promise object and not the result.

Can you please check the following:

let systemsizeactual = (await system_size_actual(userData.user_monthlybill,regionData.region._id)).toString()

Regards,
Genry.

1 Like

Hey there!
The solution was indeed the below. I don’t know I didn’t think of that in the first place. I am such a bummer sometimes.

system_size_actual(userData.user_monthlybill,regionData.region._id)
		.then(function(results) {
    		console.log(results);
    		$w("#systemsizeactual").text = results.toString();
	});