我有这样的代码......对于混乱我一直在这一段时间遗憾:代码的承诺。于是承诺之前发射的方式完成
loadAvailabilities() {
let promises = [];
let promises2 = [];
let indexi = 0;
//return new Promise((resolve, reject) => {
this.appointments = this.af.list('/appointments', { query: {
orderByChild: 'selected',
limitToFirst: 10
}});
let mapped;
this.subscription2 = this.appointments.subscribe(items => items.forEach(item => {
//promises.push(new Promise((resolve, reject) => {
console.log(item);
let userName = item.$key;
//this.availabilities = [];
for(let x in item) {
let month = x;
console.log(x + " month");
this.appointmentsMonth = this.af.list('/appointments/' + userName + '/' + month);
this.subscription3 = this.appointmentsMonth.subscribe(items => items.forEach(item => {
this.startAtKeyAvail = item.$key;
//console.log(JSON.stringify(item) + " item");
let date = new Date(item.date.day * 1000);
let today = new Date();
console.log(date.getMonth() + "==" + today.getMonth() + "&&" + date.getDate() + "==" + today.getDate());
console.log("IN LOAD AVAILABILITIES *(*((**(*(*(*(*(*(*&^^^^%^%556565656565");
if(date.getMonth() == today.getMonth() && date.getDate() == today.getDate()) {
console.log(" inside the if that checks if its today");
console.log(item.reserved.appointment + " *************appointment");
//let counter = 0;
//mapped = item.reserved.appointment.map((r) => {
//item.reserved.appointment.forEach((r, index) => {
for(let r of item.reserved.appointment) {
promises.push(new Promise((resolve, reject) => {
if(r.selected == true) {
//this.renderer.setElementStyle(this.noavail.nativeElement, 'display', 'none');
let storageRef = firebase.storage().ref().child('/settings/' + userName + '/profilepicture.png');
let obj = {'pic':"", 'salon': userName, 'time': r.time};
storageRef.getDownloadURL().then(url => {
console.log(url + "in download url !!!!!!!!!!!!!!!!!!!!!!!!");
obj.pic = url;
this.availabilities.push(obj);
console.log(JSON.stringify(this.availabilities));
resolve();
}).catch((e) => {
console.log("in caught url !!!!!!!$$$$$$$!!");
obj.pic = 'assets/blankprof.png';
this.availabilities.push(obj);
console.log(JSON.stringify(this.availabilities));
resolve();
});
}
}))
}
}
}))
}
}))
//}));
Promise.all(promises).then(() => {
console.log("in load availabilities ......... ")
console.log(JSON.stringify(this.availabilities));
this.availabilities.sort(function(a,b) {
return Date.parse('01/01/2013 '+a.time) - Date.parse('01/01/2013 '+b.time);
});
console.log('*****previous******');
console.log(JSON.stringify(this.availabilities));
console.log('*****sorted********');
for(let i of this.availabilities) {
console.log(i.time + " this is itime");
let date = new Date('01/01/2013 ' + i.time);
console.log(date + " this is date in idate");
let str = date.toLocaleTimeString('en-US', { hour: 'numeric', hour12: true, minute: 'numeric' });
console.log(str);
i.time = str;
}
});
//}))
//})
}
我可以从日志消息告诉了storageRef.getDownloadURL()
函数发生在我的页面加载结束时......这是对象实际被推到this.availabilities
(最终用于填充列表)的地方。 Promise.all
.then()
中的代码实际上在任何事件被推送到this.availabilities
之前触发,所以当发生排序时它是一个空数组,并且没有任何排序。
不要为这段代码的混乱道歉。把它扔掉,重新开始。任何想要认真回答的人都必须这样做,所以开始你会很体面。如果您发布的内容中有超过10%的内容对您的问题有所贡献,我会非常惊讶。抛出另外90%将有助于我们和你锁定它。 – Tomalak
遐我想我应该重新开始这个......我想我不知道在彼此内部的'forEach'循环..以及里面的承诺...什么是异步和什么是同步.. ..但是我要重新开始......这是我早写的代码,事情已经改变 – ewizard
一般提示。 Unnest你的代码。为每一个有意义的步骤编写专用函数。从分解最内层的东西开始。理想情况下,每个函数应保留四行代码。使用map()和reduce()。链接你的承诺。倾向于运行一个以上的地图()。循环性能不是你在异步情况下的问题,所以不要为它优化。优化平面,明显的代码。 – Tomalak