- Home /
How do I make HTTP requests work synchronously so that the code is executed after a response is received and parsed.
So I am making a POST request to our own backend and the code looks something like this:
// a method in a class for UI management
public void SubmitUser(UserInfo user) {
BackendAPI.CreateUser(user);
CheckForErrors();
}
// the static method in BackendAPI class
public static void CreateUser(UserInfo user) {
Hashtable data = new Hashtable();
data.Add("username", user.username);
Request req = new Request("post", baseUrl + "users/", data);
req.AddHeader("Authorization", authToken);
req.Send((request) => {
if (request.response.status == 422) {
MenuUIManager.usernameExists = true;
} else if (request.response.status == 200) {
MenuUIManager.userCreated = true;
}
});
}
So the CheckForErrors() method checks if the public bool variables of the instance of MenuUIManager is set depending on the response from the server.
The issue is that since it's in a callback the CheckForErrors() runs before the callback finishes executing.
So how can I make the CheckForErrors() only run once the callback inside CreateUser method completes?
Comment
Answer by Tasarran · Apr 06, 2017 at 08:39 PM
Putting CheckForErrors() at the end of CreateUser() might help?
If not, why not just put a timer in to delay calling CheckForErrors() for some short increment of time?
Your answer