Answer the question
In order to leave comments, you need to log in
How to extract JSON from Promise?
I need to get JSON into a variable. However, due to JS being asynchronous, I get a Promise there.
$scope.stat_fromdb = $http.get('http://127.0.0.1:8080/stat').success(function(response) {
// $scope.stat_fromdb = response.data;
return response.data;
console.log($scope.stat_fromdb);
});
console.log($scope.stat_fromdb);
Answer the question
In order to leave comments, you need to log in
Or take data from response inside callback
$http.get('http://127.0.0.1:8080/stat').success(function(response) {
// используйте свои данные прямо тут
var data = response.data;
console.log(data);
});
var getData = function (callMeWhenDataReady) {
$http.get('http://127.0.0.1:8080/stat').success(function(response) {
var data = response.data;
console.log(data);
// передайте данные во внешний callback
callMeWhenDataReady(data);
});
};
...
getData(function(data) {
console.log(data);
} )
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question