Question with invalid away in async function
Why is the await on the same function invalid in the error call back, but not in the success. Does the event cause a conflict? It gives the error, unexpected reserved word.
const successCallBack = async function (position) {
try {
const { latitude, longitude } = position.coords;
const coords = [latitude, longitude].toString();
await model.fetchWeather(coords);
renderWeather();
} catch (error) {
console.log(error);
}
};
const errorCallBack = async function (error) {
try {
if ((GeolocationPositionError.code = 1)) {
document.getElementById("modal").addEventListener("submit", (e) => {
e.preventDefault();
const coords = mainView.getManualCoords()
await model.fetchWeather(coords);
renderWeather();
});
}
} catch (error) {
console.log(error);
}
};
6 Replies
You are using
await
in a non-async function. The callback function to the submit event.That I understood, and it is accepting the await, but it is not waiting, the function to fetch weather is happening after the render weather. Which leads to all the data saying undefined on the page. But the data comes up after it, and is correct, just executing in wrong order.
Can you try to mark the callback function as async?
I got it working, thank you. I never new you could make event listener an async, but it makes sense.
Yeah at the end of the day they're just regular functions. Writing the function outside on its own and then just provide the reference helped me a lot to understand how this works
Once i thought of it that way after you told me, it clicked, thank you again.