I'm relatively new to Javascript and haven't been able to figure out a way to make my code wait for some result before it executes the next steps. I did some research on callbacks, but I can't seem to make it work with my code.
I am using the Flask framework to develop a webpage. The following code calls one of the endpoints to get a JSON response (name
is the parameter I'm sending to the endpoint and data
is the JSON response I get back):
$.getJSON('/if_name_exists', {
name: "sample_name",
}, function(data) {
if (data.check == "True") {
alert("Name already exists! Please enter another name.");
return false;
}
});
The problem is that Javascript doesn't wait for this response and executes the next steps in my program. This is what I've tried-
$.getJSON('/if_name_exists', {
name: "sample_name",
}, function(data) {
if (data.check == "True") {
alert("Name already exists! Please enter another name.");
return false;
}
});
(async () => { await wait(2000); console.warn('done') })();
But my code still doesn't wait for the response and executes the next steps. How do I solve this issue?