I am trying to use Promise.all
and map
instead of the forEach loop so the task can be asynchronous. All of the promises in the Promise.all
array get executed and are resolved. The code looks like this:
loadDistances() {
//return new Promise((resolve, reject) => {
let rrr;
let arr = [];
this.geolocation.getCurrentPosition().then((resp) => {
// resp.coords.latitude
rrr = resp;
console.log(rrr + " rrrrrrrrrrrrrrrrrrrrrrrrrr");
setTimeout(() => {
this.distancelist = this.af.list('/profiles/stylists');
let x = 0;
this.subscription6 = this.distancelist.subscribe(items => {
let mapped = items.map((item) => {
return new Promise(resolve => {
let rr;
//console.log(JSON.stringify(item) + " *((*&*&*&*&^&*&*&*(&*(&*&*(&(&(&*( :::" + x);
if(item.address == "") {
/*if(!item.picURL) {
item.picURL = 'assets/blankprof.png';
}*/
//arr.push({'pic':item.picURL, 'salon':item.username, 'distance':"No Address"});
//x++;
}
else {
console.log(item.address + " is the address empty??????");
this.nativeGeocoder.forwardGeocode(item.address)
.then((coordinates: NativeGeocoderForwardResult) => {
console.log("I AM IN THE GEOCODING ***&&*&*&*&*");
rr = this.round(this.distance(coordinates.latitude, coordinates.longitude, rrr.coords.latitude, rrr.coords.longitude, "M"), 1);
if(!item.picURL) {
item.picURL = 'assets/blankprof.png';
}
arr.push({'pic':item.picURL, 'salon':item.username, 'distance':rr});
console.log("push to the array of results");
//x++;
/*console.log(items.length + " length / x: " + x);
if(items.length - x == 1) {
console.log("getting resolved in geocoder ^&^&^&&^^&^&^&");
resolve(arr);
}*/
resolve();
}).catch(e => {
console.log(e.message + " caught this error");
/*x++;
if(items.length - x == 1) {
resolve(arr);
}*/
resolve();
})
}
})
});
let results = Promise.all(mapped);
results.then(() => {
console.log(JSON.stringify(arr) + " :FOSIEJO:SFJ::EFIJSEFIJS:EFJS:IO THIS IODIOSJ:FDSIJ :DIS");
arr.sort(function(a,b) {
return a.distance - b.distance;
});
this.distances = arr.slice();
})
});//);
}, 1500)
/*}).catch((error) => {
this.diagnostic.switchToLocationSettings();
console.log('Error getting location', error.message);
resolve();
});*/
});
}
The console output is:
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:27] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:27] console.log: push to the array of results
[12:38:28] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:28] console.log: push to the array of results
[12:38:29] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:29] console.log: push to the array of results
[12:38:29] console.log: I AM IN THE GEOCODING ***&&*&*&*&*
[12:38:29] console.log: push to the array of results
The alternating messages make it seem like it worked and all of the promises got resolved. But when I do Promise.all
this line never happens in the console:
console.log(JSON.stringify(arr) + " :FOSIEJO:SFJ::EFIJSEFIJS:EFJS:IO THIS IODIOSJ:FDSIJ :DIS");
So it is not reaching the then
of the Promise.all
results
.