Sunday 11 August 2019

javascript - JS : How to use return type of callback function for returning the main callee function



I found few good links (How to return value from an asynchronous callback function? and Returning a value from callback function in Node.js etc) of SO but they are not not able to provide solution to my problem.



My Problem: able to get result of asynchronous call, but how I can use this result for returning of my function?



onsubmit = "return checkForm(function(callBackResponse) { alert(callBackResponse) });"



Here getting value of callBackResponse either as true or false, and want to use this value as :



onsubmit = "return responseFromCallBack;"


EDITED : Here is my async stuff



function checkForm(callback) {return firstCallBack(function secondCallBack(response) {
if (some conditions) {
response = false;

callback(response);
}
else {
response = true;
callback(response);
}
}
);
}


Answer



If i get ur issue correctly:
you want to "validate" (checkForm()) before submitting?!
in this case set an event listener to the form submit-event...
prevent (event.preventDefault()) the default behavior (submit) call your function (checkForm()) if everything is fine, trigger the submit manually ;)
e.g. document.getElementById('myFormId').submit()



Cheerio :)


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...