Get data out of a promise instead of returning a promise

后端 未结 2 2029
别那么骄傲
别那么骄傲 2021-02-05 14:49

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

2条回答
  •  情深已故
    2021-02-05 15:39

    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.

提交回复
热议问题