Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have an issue in to write javascript asynchronous functions with ajax, my problem is that I have written 3 function with return boolean.
JavaScript
function getname(a,b)
{
.. $ajax here 
 {
    success(data)
       {
         if (data.resultvalue == true){
            return true;
         }
         else
         {
            return false;
         }
      }
   }
}
=============================================================
function validate(a)
{
.. $ajax here 
  {
     success(data)
        {
          if (data.resultvalue == true){
             return true;
          }
          else
          {
             return false;
          }
       }
  }
}
=============================================================
function checken(a)
{
.. $ajax here 
  {
     success(data)
        {
          if (data.resultvalue == true){
             return true;
          }
          else
          {
             return false;
          }
       }
  }
}
=============================================================
function callallfunctions()
{
   var aval = document.getelementbyid('myval');
   if (getname(aval.value,'abc') == true)
   {
      if (validate('abc') == true)
      {
         if (checken('def') == true)
         {
            return true
         }
         else
         {
            return false;
         }
      }
      else
      {
         return false;
      }
   }
   else
   {
      return false;
   }
}

Its working asynchronously and return value false while ajax is under processing. When i merge all the functions in one function then its working asynchronously too and return value false while ajax is under processing
Posted
Updated 13-Feb-15 4:03am
v3
Comments
ZurdoDev 13-Feb-15 9:50am    
What is your question?

Since it runs asynchronously you can't return a value because the function ends before your ajax calls finishes. You need to re-write your code so that in success something happens instead of trying to return a value.

As Ryan said, you can't return a value directly, because the function returns before the asynchronous AJAX method has completed.

You need to re-write your code to use a more asynchronous approach. For example:
JavaScript
function getname(a, b)
{
    return $.ajax(...);
}

function validate(a)
{
    return $.ajax(...);
}

function checken(a)
{
    return $.ajax(...);
}

function callallfunctions()
{
    var aval = document.getelementbyid('myval');

    return getname(aval.value, 'abc')
        .then(function(getnameResult){ return !!getnameResult.resultvalue ? validate('abc') : false; })
        .then(function(validateResult){ return !!validateResult.resultvalue ? checken('def') : false; })
        .then(function(checkenResult){ return !!checkenResult.resultvalue });
}

This uses jQuery's implementation of JavaScript Promises[^] to chain the asynchronous calls together.

Your calling code can then use the .done()[^] and .fail()[^] methods to register functions which will be called when all three methods have been completed, or when one of the methods fails.
JavaScript
callallfunctions()
    .done(function(result){ alert(result); })
    .fail(function(){ alert("Failed"); });
 
Share this answer
 
Comments
adeez 13-Feb-15 15:47pm    
Thank you so much Richard Deeming its working :)
example is here http://api.jquery.com/deferred.done/
Sergey Alexandrovich Kryukov 13-Feb-15 16:11pm    
5ed.
—SA
Sorry, but, to start with, it looks like you are clueless about basics of programming, not only extremely flexible JavaScript programming. Look:
C#
if (checken('def') == true)
{
   return true
}
else
{
   return false;
}

This is the same as
JavaScript
return chechen('def') // I have no idea what is "chechen"

Isn't that obvious? And so on…

So, first, you have to get such basics things and write code in humane manner. This is not really hard to do. Only then we can discuss something else.

—SA
 
Share this answer
 
Comments
Richard Deeming 13-Feb-15 12:16pm    
Not precisely true in javascript. If checken('def') returns something that isn't a boolean, the longer version converts it to a boolean depending on its "truthiness", whereas the shorter version returns it unchanged.

The equivalent one-liner would be:
return !!checken('def');
Sergey Alexandrovich Kryukov 13-Feb-15 12:29pm    
Oh, good point and the pattern. Agree.

Just a note: as the intent was to return true or false object, the intent for the use of the return value could be using it in another "if". And the ultimate "if" will eliminate this difference. So, in practice, the ultimate result will be the same. Agree?

I think the JavaScript development philosophy is rather ignoring the difference between different objects null, undefined, 0, false and other objects behaving identically under "if". In other words, even though if (something === null) or if (something === true) (the opposite, most strict check) can be used to detect the difference between undefined, true, null or any other objects, consistent use of if (something) will still give consistent results. This is a matter of individual approach, but the "if" techniques should be consistent.

Thank you for this important note.

—SA
adeez 13-Feb-15 15:51pm    
thank you Sergey Alexandrovich Kryukov and all members for fast response :)
actually upper define functions are just for examples

return chechen('def') // I have no idea what is "chechen"

checken is function name I used it just for example and I know I can't do it without any callback function but may be I am clueless about basics of javascript programming :)
Sergey Alexandrovich Kryukov 13-Feb-15 16:11pm    
All right, fix your code appropriately...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900