I make a function to check the correctness of the data when sending the form There is a variable error with error numbers. If error == 0, then send the form, if not, then output the corresponding error and return false. Inside the function there is a $ .get-function that does one of the checks (and if the data is incorrect, it changes the value of error). Problem: since the function is asynchronous, the assignment of a variable occurs already after the form is sent (that is, the check does not work). Question: how to pass the error value BEFORE inside the function check goes to error == 0. Or how to pause the form submission before the $ .get function is processed?

$("#button").submit(function () {// обрабатываем отправку формы var error = 0; // индекс ошибки ... //первая проверка (классическая, с ней всё хорошо) if (a > b) { error = 1; } ... $.getJSON('json.json', function (data) { ... if (...){ setResult("2"); // присваиваем error значение 2 } ... }); //присваиваем значение внутри get-функции function setResult(result) { error = result; } ... дальше проверки error, и return true, если error == 0 или вывод ошибок + return false. 

PS There is a wonderful answer here: How can I return a value from an event or from a callback function? - but something didn’t really help, because I don’t just have to pass the value, but pass it BEFORE checking for error == 0 and sending the form.

Reported as a duplicate at Grundy. javascript Apr 20 '17 at 8:40 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • I did not understand what exactly should be suspended until what? - Grundy
  • While closed as a duplicate, edit your question by adding more details and what exactly you tried. Try to make a minimal reproducible example - Grundy
  • @Grundy I need not assign a value (this happened - the setResult function is just taken from that question (Method 0+)), and for this value to be assigned BEFORE the form is sent (or somehow suspend the form is sent until how the assigned value will return). I think this is a slightly different question. I don’t know how to make a reproducible example with a json file that is on a computer ... - Ya_O
  • In fact, you did not quite correctly understand what that function should do. In this question, the emphasis on that is done, that all actions that should be after the request should be performed in a callback. And here you only assign the value of a variable in a callback, and you cannot tell whether you assign one or another to the given code. Plus, it is not known exactly how submit. Therefore, take an example a little closer to your code, removing what is not relevant to the question. - Grundy

1 answer 1

I slightly corrected, if the idea is only to show the user whether the form was sent, then function (data) {is the callback, in it we can check the json data for an error, if not an error, show the client that everything is ok, highlight a modal window or an alert with the words "Form successfully sent", if at least one of the checks in the chain introduced an error, then do something else:

 $("#button").click(function (event) { var error = false event.preventDefault(); if (a > b) error = true; ..... $.getJSON('json.json', function (data) { $.each( data, function( key, val ) { if (...) { error = true } }); if (!error) { //Все ок! .....//Тут что-нибудь делаем alert('Form was send!'); } else { //Где-то в цепочке проверок вылезла ошибка alert('Error!'); } }); } 
  • Thanks, I will try to work with a click, and not with submit. Just do not quite understand how to make checks out of GET? I get and call only for checking (if the value is already in the json-file, then I change the error value). But in this case, the error value in this case changes after the form has been submitted. - Ya_O
  • I understand now that you have a mistake in logic. You first get data from get, then you can check it. What do you need to do with the result synchronously? - larrymacbarry
  • Yes, I wanted to. Get data from get, check if this value already exists in the json file, and if so, deny sending the form. Asynchrony hinders me in this test. The order is as follows: 1) Receive data from get 2) process them 3) change the value of error, if necessary 4) Check the value of error and (if error! = 0) prevent the form from being sent. And I have 3 points after the 4th. - Ya_O