[英]Creating a function that returns a promise and handle the callback
这是我的代码:
function getUserRatingsSorted(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 prepareGameForStart(userIds) {
getUserRatingsSorted(userIds)
.then((users) => evaluateUsersByRatings(users))
.then((playerAndRatings) => distributePlayersInTeams(playerAndRatings))
.then(notification.sendPushNotificationTo('Teams have been selected', 'Log in to your group to see your team', userIds))
.catch((error) => {
console.log(error);
});
}
当我登录的用户在第一然后我得到
[[功能],[功能],[功能],[功能]]
并且会在从Firebase检索用户之前触发它。 因此,如果我在getUserRatingsSorted
函数中console.log(user)
,则在用户登录后将其打印出来。 对我来说,这听起来并不正确,因为事情的承诺应印有那么事情在当时 。 我相信我在创建Promise函数时做错了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.