2

Is it possible to safely wait for async request to finish and then return value from function. Will something like this cause any problems.

Using jQuery

function test()
{
    var $request = ...; //Async promise

    var done = false;
    while(!done)
    {
        $request.always(function(){
             done = true ;
        }) ;
    }

    return true ;
}

[Update] As @Ninsly pointed out, javascript only has one thread so using a loop is not an option.

Duplicate JavaScript asynchronous return value / assignment with jQuery

Community
  • 1
  • 1
d0001
  • 2,162
  • 3
  • 20
  • 46
  • 1
    just return the promise. you can't wait for async. – Daniel A. White Nov 21 '14 at 19:10
  • I know I can return the promise. But I am looking for cases where test() is already in use and must return a value. I don't want to refactor anything else so I would like to know if this is the best solution. – d0001 Nov 21 '14 at 19:12
  • it sounds like a refactoring is in order. – Daniel A. White Nov 21 '14 at 19:13
  • 4
    Javascript only has 1 thread. You can't *wait* for another thread to finish in your current thread, because there's only one thread! – Stryner Nov 21 '14 at 19:14
  • actually it depends on what you want to do to handle this...as Ninsly sayed you cant wait. But you can hold the ajax back until something other is ready... some like `$(document).ready(function(){ ...your ajax... });` than the async doesnt matters anymore.. but like i sayed... it depends on what you want to do. this doesnt work in all cases – Dwza Nov 21 '14 at 19:26
  • I would like a function to return a value that depends on some async requests. Not a promise but the actual value. – d0001 Nov 21 '14 at 19:28
  • could you give us a sample for this ? – Dwza Nov 21 '14 at 19:29
  • @Dwza I have defined a function test above. I would like to do something like this: var a = test() ; where the return value was calculated using async requests like ajax calls. – d0001 Nov 21 '14 at 19:31
  • You can't do that Daniel. Async is asynchronous. That means it will start in the background and finish sometime later and your function will return immediately and your javascript will keep executing. That asynchronous. Please read and study [the question](http://stackoverflow.com/questions/7779697/javascript-asynchronous-return-value-assignment-with-jquery) that yours was marked a duplicate of. You will HAVE to learn how to do some async programming and the selected answer to that question gives you lots of options for how to do that. – jfriend00 Nov 21 '14 at 20:16

2 Answers2

1

You could use a synchronous Ajax request. Specify the async option to be false to get a synchronous Ajax request. Then your callback can set some data before your parent function proceeds.

test: function(){
    jQuery.ajax({
         url:    '...',
         success: function(response) {
                      if(response.bOk == false)
                          alert(response.content);
                          //you could return some here
                  },
         async:   false
    });          
}

Take care, if you would use this, you block/lock all other processings until its done... Same like doing a endless while in PHP. So if you request doesnt ends.. this will cause a little problem :)

Dwza
  • 6,494
  • 6
  • 41
  • 73
  • FYI, synchronous Ajax requests are highly, highly discouraged because they lock up all other processing of events in the browser. – jfriend00 Nov 21 '14 at 19:47
  • same as you mess up php ^^ but security and locking op other processes wasnt a part of op's question. but thx for pointing this out, i will add this to the post. – Dwza Nov 21 '14 at 19:51
-1

you should use a callback in the async function.... smth like jQuery .done

$request.done(function( data ) {
  console.log("ready");
})
Alexis Peters
  • 1,583
  • 1
  • 10
  • 17