How do I use it in a getter function that assigns it to a variable :
get aws_total_cost()
{
let vm_total = global_get_aws_instance_pricing(Alpine.store('global_data').aws['location'], Alpine.store('global_data').aws['instance']);
return vm_total;
},
problem is, my editor says âThe âawaitâ operator can only be used in an âasyncâ functionâ but Unfortunately, JavasScript syntax does not support asynchronous getters.
Youâre correct that JavaScript doesnât support asynchronous getters. However, you can work around this by returning a Promise from your getter and then handling it asynchronously where you use it.
get aws_total_cost()
{
return global_get_aws_instance_pricing(Alpine.store('global_data').aws['location'], Alpine.store('global_data').aws['instance']);
}
Then, when you use aws_total_cost , you handle it as a Promise:
this.aws_total_cost.then(vm_total => {
// You can use vm_total here
});
This way, youâre not trying to await the Promise inside the getter (which isnât allowed), but instead youâre returning the Promise from the getter and then awaiting it where you use the getter. I hope this helps.
The issue Iâm facing is that I canât seem to return the result from the promise
get aws_total_cost()
{
this.aws_vm_cost.then(vm_total =>
{
let total_cost = (
parseFloat(vm_total) * 5
).toFixed(2);
return total_cost; // This is not getting returned
});
},
I am glad you find it this way, I would highly recommend you to add a .catch() block. By doing this can handle any errors that might occur during the execution of your Promise. Best of Luck!!