I am strugling to find a solution to the following problem.
We need to submit the form on our website, on submit, the website sends data to google and once the google function returns the data was "processed", the form should submit.
To make sure the customer does not wait for ages, we wanted to add a backup plan - when google does not reply in 5 seconds, just form.submit() and move on.
Sadly this does not work very well, my question is, would it be possible to rewrite this code to work like:
- send data to google and also wait for 5s
- No matter which of these events come first, await the return value and move on.
let formSubmitted = false;
function onCheckout(form) {
setTimeout(() => {
submitForm(form)
}, "5000");
sendToGoogle(submitForm(form));
//send data to google, run submitForm() on callback, this is just simplified function
// here I would like to return the value of formSubmitted, after max 5s.
}
function submitForm(form) {
if (!formSubmitted) {
formSubmitted = true;
form.submit();
}
}