简体   繁体   中英

nodejs async func / promisse

Hi I'm trying to make a queue system (similar to the fetching system in cs go, lol etc)

Basically to create a match I need two Players

so I joined as player one with a 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

if I don't have this player queued I must remain in queue until player 2 enters in the queue

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

Do you have to use promises? If not, make functions as getFirstPlayer and getSecondPlayer . Then call getSecondPlayer in callback of getFirstPlayer .

Because you don't have promise in your code.

您可以使用Async系列来完成工作,请点击此处

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM