[英]nodejs async func / promisse
Hi I'm trying to make a queue system (similar to the fetching system in cs go, lol etc) 嗨,我正在尝试制作一个队列系统(类似于cs go,lol等中的获取系统)
Basically to create a match I need two Players 基本上要进行比赛,我需要两名选手
so I joined as player one with a 1000 mmr 所以我以1000 mmr的身分加入了
i am in line and so i need to wait for my queue to have a two player that has between 800 and 1200 mmr 我在排队,所以我需要等待队列中有两个播放器,播放器的mmr在800到1200之间
if I don't have this player queued I must remain in queue until player 2 enters in the queue 如果我没有将此玩家排队,那么我必须一直待在队列中,直到玩家2进入队列为止
my main: 我的主要:
const players = [
{ id: 1, name: 'spt', mmr: 970 },
{ id: 2, name: 'test2', mmr: 1000 },
{ id: 3, name: 'test3', mmr: 1050 },
{ id: 4, name: 'test4', mmr: 70 }
]
queue.addPlayers(players);
const makeMatch = queue.makeMatachx(4);
makeMatachx(id){
const firstPLayer = this.players.find(p => p.id == id)
const { mmr } = firstPLayer
const secondPlayer = this.players.find((playerTwo) => playerTwo.mmr < (5 / 100) * mmr + mmr && playerTwo.mmr > mmr - ((5 / 100) * mmr) && playerTwo.id != playerDetails.id);
}
I'm a little lost I think just using prommise to solve this but I don't know how 我有点迷茫,我想只是用Promise解决了这个问题,但我不知道如何
Do you have to use promises? 您必须使用诺言吗? If not, make functions as getFirstPlayer and getSecondPlayer .
如果不是,则将函数设为getFirstPlayer和getSecondPlayer 。 Then call getSecondPlayer in callback of getFirstPlayer .
然后在getFirstPlayer的回调中调用getSecondPlayer 。
Because you don't have promise in your code. 因为您的代码中没有承诺。
您可以使用Async系列来完成工作,请点击此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.