14

I am so sorry if the other promise threads have answered this but when looking at some of them I am just not getting the answer to solve my issue. I have three json files that I want to grab, parse and manually merge. The problem is I am getting stuck in promise jail. Let me show you some of the code from my angularjs controller.

$scope.tests = [];

$scope.tests = $http.get('results/testResults.json').then(function(res) {
  return res;
});

console.dir($scope.tests);

From the console.dir I am getting a promise but what I was hoping for was the data from the res variable. There has to be some way to get that data out. Is there no way to get that data out of the promise to a global variable so other promises of functions can use this data? Thanks

jrock2004
  • 3,229
  • 5
  • 40
  • 73
  • 1
    you don't get data out of callbacks, you use the data in the callback – dandavis Nov 10 '14 at 23:02
  • .then() your last promise and console log there – Patsy Issa Nov 10 '14 at 23:04
  • This is the exact wrong way to use promises. Promises are asynchronous and you are trying to use it synchronously. – greggreg Nov 10 '14 at 23:04
  • [Promises are "just" asynchronous callbacks](http://stackoverflow.com/a/22562045/1048572), and [asynchronous callbacks cannot do that](http://stackoverflow.com/q/23667086/1048572) – Bergi Nov 10 '14 at 23:06

2 Answers2

18

The promise completes some time in the future. You are examining the promise variable before the data is in the promise. You should stay in the promise chain to use your data. Outside the promise chain, you don't know the timing of the asynchronous events (that's why you use promises in the first place).

If you really don't want to use the data right in your first .then() handler which is the ideal place to use it, then you can chain another .then() onto your promise:

$scope.tests = $http.get('results/testResults.json');

$scope.tests.then(function(data) {
   // can use data here
});

FYI, promises do not populate data into global variables. They make the data available to use in .then() callbacks when those callbacks are called.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • You'll also want to eliminate that identity mapping. – Bergi Nov 10 '14 at 23:07
  • 1
    So I cannot build one big object and collect the data from three json files and merge? To me it seems like I am stuck – jrock2004 Nov 10 '14 at 23:13
  • 1
    @jrock2004 - you have to think and write code a bit differently when using asynchronous operations. You can use other promise features such as `Promise.all()` to coordinate multiple asynchronous operations so you can run some code when all the async results are done and use all the results. – jfriend00 Nov 10 '14 at 23:16
-2

Make up an an property and stick it on the window object, like this,

window.myProperty

Then pass your data to it, from inside the promise, like this,

window.myProperty = data;

And pick it up, when you are back outside, like this,

myVariable = window.myProperty;

Or the reverse, because I cannot seem to get data in or out of some Promises, so that would go like this,

Again, make up a property and stick it on the window object, like this,

window.myProperty

Then pass your data to it, from outside the promise, like this,

window.myProperty = data;

And pick it up, when you are inside, like this,

myVariable = window.myProperty;

This cannot be the best way to do this! Does anyone know a better way?