Sunday, August 11, 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

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...