我有這樣的代碼......對於混亂我一直在這一段時間遺憾:代碼的承諾。於是承諾之前發射的方式完成
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