0
的我實現以下兩個承諾:的差異的方式返回承諾
function getUserSportListPromise(_uid){
return firebase.database().ref('users/' + user.uid + '/sports/').once('value').then(function(snap){
var userSportList = [];
if(snap.exists()){
snap.forEach(function(sport){
userSportList.push(sport.val().name);
});
}
return userSportList;
});
}
function getRegistrationsFromSportList(sportList){
var promiseHash = {};
sportList.forEach(function (sport){
promiseHash[sport] = firebase.database().ref('/sport_registrations/' + sport).once('value');
});
return $q.all(promiseHash);
}
爲什麼這段代碼顯示結果:
var userSportListPromise = getUserSportListPromise(user.uid);
var registrationHashPromise = userSportListPromise.then(function(sportList){
return getRegistrationsFromSportList(sportList);
}).then(function(registrationsHash){
angular.forEach(registrationsHash, function(regsSnap, sport){
console.info(sport + ' has the following registrations: ');
console.dir(regsSnap);
});
});
鑑於此代碼顯示undefined:
getUserSportListPromise(user.uid)
.then(function(sportList){
getRegistrationsFromSportList(sportList);
})
.then(function(registrationsHash){
angular.forEach(registrationsHash, function(regsSnap, sport){
console.dir(registrationsHash);
console.info(sport + ' has the following registrations: ' + regsSnap);
});
});
函數getUserSportListPromise返回一個promise的散列,爲什麼上面的代碼傳遞一個undefine d在last()調用中註冊哈希?
事實上,謝謝! –