-1

I'm trying unsuccessfully pass my array to the outside function as a result of it? Here is working code in jsbin: https://jsbin.com/kugesozawi/edit?js,console,output The result should be passed to returnSearch.

var googleSuggest = function(returnSearch){

  var term = $('#searchInput').val();
  var result = [];
  var service = {
    youtube: { client: 'youtube', ds: 'yt' },
    books: { client: 'books', ds: 'bo' },
    products: { client: 'products-cc', ds: 'sh' },
    news: { client: 'news-cc', ds: 'n' },
    images: { client: 'img', ds: 'i' },
    web: { client: 'hp', ds: '' },
    recipes: { client: 'hp', ds: 'r' }
  };

  $.ajax({
    url: 'https://clients1.google.com/complete/search',
    dataType: 'jsonp', 
    data: {
      q: term,
      nolabels: 't',
      client: service.web.client,
      ds: service.web.ds
    } 
  }).done(function(data) {

      result.pop()

      $.each(data[1], function(item, value) {

        var stripedValue = value[0].replace(/<[^>]+>/g, '');

           result.push(stripedValue); 

      })

      console.log(result)

    })

  returnSearch = ['ActionScript', 'AppleScript', 'Asp']

  return returnSearch

};
Spyder
  • 109
  • 1
  • 2
  • 11
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Frxstrem Apr 02 '17 at 00:19

1 Answers1

-1

I think the only way to make it return the value you have in mind is to make the call synchronous, which means that whenever the call ajax call is made, you will have to wait for it to be ready (what you generally don't want). You could call the returnSearch function from within the result call like this:

  var googleSuggest = function(returnSearch){
  var term = $('#searchInput').val();
  var service = {
    youtube: { client: 'youtube', ds: 'yt' },
    books: { client: 'books', ds: 'bo' },
    products: { client: 'products-cc', ds: 'sh' },
    news: { client: 'news-cc', ds: 'n' },
    images: { client: 'img', ds: 'i' },
    web: { client: 'hp', ds: '' },
    recipes: { client: 'hp', ds: 'r' }
  };

 var promise = $.ajax({
    url: 'https://clients1.google.com/complete/search',
    dataType: 'jsonp', 
    data: {
      q: term,
      nolabels: 't',
      client: service.web.client,
      ds: service.web.ds
    } 
  })

  return promise
};


$(function() {  
  $('#searchInput').autoComplete({
    minChars: 1,
    source: function(term, suggest){
      var promise = googleSuggest()
      returnSearch = function(term, choices) {
        console.log(choices)
        var suggestions = [];
        for (i=0;i<choices.length;i++)
         if (~choices[i].toLowerCase().indexOf(term)) suggestions.push(choices[i]);
          suggest(suggestions);
      }

      $.when(promise).then(function(data) {
        term = term.toLowerCase();
        var result = [];
        $.each(data[1], function(item, value) {
          var stripedValue = value[0].replace(/<[^>]+>/g, '');
             result.push(stripedValue); 
        })
        returnSearch(term, result)
      })
    }
  });  
})

Also see this stackoverflow post.

Community
  • 1
  • 1
frbl
  • 1,172
  • 11
  • 17
  • 1
    The better alternative is to make the outer function asynchronous, as any long-running synchronous actions (such as fetching a resource on the server) will block the event loop and make the page unresponsive. While making the fetching synchronous is easier, it's generally bad practice for this reason. – Frxstrem Apr 02 '17 at 00:22
  • Perfect this is working like a charm! Thanks mate...I need to learn promises more. @ Frxstream could you suggest your alternative? – Spyder Apr 02 '17 at 00:23
  • 1
    @Spyder Look at the question I linked to in my comment to your question; it explains what I'm talking about. – Frxstrem Apr 02 '17 at 00:25
  • @Frxstrem I'm looking on this examples and this is a very long debate with many examples. Most of them are related to the callback of $ajax call. My Ajax call is already inside googleSuggest() so I'm not sure what you mean. Here is updated jsbin https://jsbin.com/fiyigunuka/1/edit?js,console,output would you mid to show me on the real example pls. – Spyder Apr 02 '17 at 00:37