我正在尝试使用Promise.all
和map
而不是forEach循环,因此任务可以是异步的。 Promise.all
数组中的所有promise都会被执行并得到解决。代码如下所示:
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();
});*/
});
}
控制台输出是:
[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
交替的消息使它看起来像是有效的,所有的承诺都得到了解决。但当我Promise.all
时,这条线永远不会出现在控制台中:
console.log(JSON.stringify(arr) + " :FOSIEJO:SFJ::EFIJSEFIJS:EFJS:IO THIS IODIOSJ:FDSIJ :DIS");
所以它未达到then
Promise.all
的{{1}}。
答案 0 :(得分:2)
您的代码路径无法解决,具体而言:
if(item.address == "") {
/*if(!item.picURL) {
item.picURL = 'assets/blankprof.png';
}*/
//arr.push({'pic':item.picURL, 'salon':item.username, 'distance':"No Address"});
//x++;
}
答案 1 :(得分:0)
在创建新的Promise时,很容易陷入像@ andy-gaskell所指出的僵局。为了避免这种情况,你最好做其中一个;所有这些都可能引发一个不可能的错误或返回1.
function promiseWithTryCatch() {
return new Promise((resolve, reject) => {
try {
let result;
// your code
resolve(result);
}
catch(ex) {
reject(ex);
}
})
}
function promiseWithResolve() {
return Promise.resolve()
.then(() => {
let result;
// your code
return result;
})
}
async function promiseWithAsync() {
let result;
// your code
return result;
}
用您的代码替换注释,并将结束结果放入变量结果中。如果您的代码包含异步代码,则最好使用相同的模式创建一个新函数并将其作为结果返回,例如:result = [promise method]
使用此示例,但我必须注释掉async / await,因为此代码段工具无法处理ES2016。
function nestedPromise(num) {
return Promise.resolve(' myNestedValue is ' + num);
}
function promiseWithTryCatch() {
return new Promise((resolve, reject) => {
try {
let mynumber = 2 + 5;
nestedPromise(mynumber)
.then((answer) => {
resolve(answer.trim());
})
.catch(ex => {
// error handling for async-code
reject(ex);
})
}
catch(ex) {
// error handling for sync-code
reject(ex);
}
})
}
function promiseWithResolve() {
return Promise.resolve()
.then(() => {
let mynumber = 2 + 5;
return nestedPromise(mynumber);
})
.then((answer) => {
// do something with the answer
return answer.trim();
})
}
/*
async function promiseWithAsync() {
let mynumber = 2 + 5;
let answer = await nestedPromise(mynumber);
return answer.trim();
}
*/
promiseWithTryCatch()
.then(answer => console.log('promiseWithTryCatch result is ' + answer))
.catch(err => console.log('promiseWithAsync error is ' + err.message));
promiseWithResolve()
.then(answer => console.log('promiseWithResolve result is ' + answer))
.catch(err => console.log('promiseWithResolve error is ' + err.message));
/*
promiseWithAsync()
.then(answer => console.log('promiseWithAsync result is' + answer))
.catch(err => console.log('promiseWithAsync error is ' + err.message));
*/