Is there a way to set values in an array from an asynchronous function and access it outside of that function's scope?
I am working on a project where I need to randomize values in an array, so that I can assign images randomly to different div elements.
$scope.Resources = ResourceFetch.getResource().then(function(resources){
$scope.img1 = JSON.stringify(resources.img1);
$scope.img2 = JSON.stringify(resources.img2);
$scope.img3 = JSON.stringify(resources.img3);
$scope.img4 = JSON.stringify(resources.img4);
var myArray = [$scope.img1, $scope.img2, $scope.img3, $scope.img4];
console.log('myArray is ' + myArray); // <- Working as expected.
var shuffledArray = [];
ShuffleArray.getShuffled(myArray).then(function(array){
shuffledArray = array;
console.log('Shuffled array is ' + array); // <- Working fine.
console.log('Shuffled array again ' + shuffledArray); // <- Everything seems good here
});
console.log('Shuffled array accessed from outside ' + shuffledArray); // <- Nothing displayed.
});