See the code snippet I wrote below:
function retrieveAndSortUserRatings(userIds) {
return Promise.all(userIds.map(id => {
return admin.database().ref().child('users').child(id).on('value', (s) => {
const user = s.val();
user.id = id;
return user;
});
}));
}
function organizeGameParticipants(userIds) {
retrieveAndSortUserRatings(userIds)
.then((users) => evaluateUsersByRatings(users))
.then((playerAndRatings) => distributePlayersInTeams(playerAndRatings))
.then(notification.sendPushNotificationTo('Teams have been chosen', 'Check your group to view your team', userIds))
.catch((error) => {
console.log(error);
});
}
After logging users in the initial then, I noticed that it displays
[ [Function], [Function], [Function], [Function] ]
This occurs before the users are retrieved from firebase. If I try to console.log(user)
within the retrieveAndSortUserRatings
function, they only appear after the users are logged. It seems incorrect as per my understanding that items in the promise should be printed prior to those in the then. I suspect there might be a mistake in how I created the promise function.