0

I need to grab the value from a route using $.get. I got it but I cannot wait up to all the others functions be finished to return the value.

function myFunction(str) { // str is my word to be searched.
    var obj = {str};
    $.get('/pronunciation/words'+'?'+$.param(obj), function( data ) {
    // I would like to pass data straightway after the line above. 
    return data;
});
janfitz
  • 1,183
  • 12
  • 21
  • That's not how `$.get` works - it's asynchronous. see here for more info: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323 – freedomn-m Jun 12 '17 at 07:43
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – freedomn-m Jun 12 '17 at 07:43
  • Thanks @freedomn-m for pass me the link, I am still struggle thinking how to fix that issue. I cannot see another way to handle it. – Jose Soares Jun 13 '17 at 02:54
  • Ok. In your `myFunction`, return the $.get, ie `return $.get(...` then in your calling function, use `.done()` - ie `myFunction("hello").done(function(data) { .... });`. Otherwise you need to rethink your design. – freedomn-m Jun 13 '17 at 07:39

0 Answers0