0

I have multiple scripts with the same object which uses the jQuery.GET to fetch some data.

crawler1.js ->

var crawlers = crawlers || {};

crawlers.Crawler1 = {
  getData : function(cb) {
    $.get('linkToSite').done(function(data){
        cb(data);
      }).fail(function(err){
          cb(false);
      });
  }
};

In app.js I have my "MainController" which needs to just receive this data.

So far I have this:

var tempData;
crawlers['Crawler1 '].getData (function(data) {
    tempData = data;
});

console.log(tempData);//This is UNDEFINED

Now I know this returns "undefined" because I'm using Asynchronous call and you don't know when it will return the data.

Is there a way to get this data out of the callback ?

I need it out of it and not something like this:

crawlers['Crawler1 '].getData (function(data) {
    tempFunc(data);
});
tempFunc(data){
    console.log(data);
}
Beni
  • 149
  • 2
  • 13
  • AngularJS has [`$http` service](https://docs.angularjs.org/api/ng/service/$http) – Aleksey Solovey Jan 17 '18 at 14:33
  • Can you add a callback to the getData() function, so: getData(data => tempData = data, () => console.log(tempData)); like you would in say, React? – rrd Jan 17 '18 at 14:45

0 Answers0