I created a Javascript function that returns a boolean value. One of the lines of code in the function invokes a REST call. I didn't realize that call was going to be asynchronous when I coded it. So, basically, the method always returns 'false' when I do something like:
if(isDataValid()) ...
I could move the actions in the calling function to the success function, but I was trying to create a reusable method. I'm using jQuery and Everest. I have not learned anything about Promises yet and I'm fairly new to callback programming.
Here's boiled down version of my method:
function isDataValid(){
// Read data from Local Storage
// Create object from data
// Flag variable to capture result
var isValid = null;
restClient
.read('/my/url', model)
.done(function(response)
{
// 'Good' response, set flag to true
isValid = true;
})
.fail(function(response)
{
// 'Bad' response, set flag to false
isValid = false;
});
return isValid;
}
How should I make an async call and return true/false?