title: Node.js async function with while loop and array push
body: <p>I hope someone can help me with this. This is part of my Controller having an async function to get some reservations. The await Calls to my Overview-Model resolve in promises. The problem is the while-loop I guess. It should look for reservations for all the hours between the opening and closure but when I console.log it at the end, I just get the last line and same hour x times.</p>
  <pre><code>while (time_ &lt; clubClosure) {
              const res_ = []
              for (const court of courts) {
                  const result = await Overview.getReservation(court.court_id, time_)
  
                  const reservation = new Reservation('free', null)
                  if (!result.kind) {
  
                      if (result.tournament_name) {
                          const tournament = new Tournament(result.tournament_name)
                          reservation.tournament = tournament
                          reservation.type = 'tournament'
                      } else if (result.opponent) {
                          const matchday = new Matchday(result.team_name, result.opponent)
                          reservation.matchday = matchday
                          reservation.type = 'matchday'
                      } else {
                          const members_ = await Overview.getMembers(result.reservation_id)
                          const members = []
                          reservation.type = 'regular'
                          for (const member_ of members_) {
                              const member = new Member(member_.first_name, member_.last_name, member_.trainer, member_.reserver)
  
                              if (member.trainer == 1) {
                                  reservation.type = 'training'
                              }
                              members.push(member)
                          }
                          reservation.members = members
  
                      }
                  }
                  res_.push(reservation)
              }
              const time = new Time(time_)
              time.reservation = res_
  
              reservations.push(time)
              time_.setHours(time_.getHours() + 1) // sets time to next hour
          }
          console.log(reservations) // prints last line x times
          res.status(200).json({
              courts,
              reservations
          })
  </code></pre>
  <p>Output (same hour x times):</p>
  <pre><code>[
    Time {
      start: 2021-06-06T22:00:00.000Z,
      reservation: [ [Reservation], [Reservation], [Reservation], [Reservation] ]
    },
    Time {
      start: 2021-06-06T22:00:00.000Z,
      reservation: [ [Reservation], [Reservation], [Reservation], [Reservation] ]
    },
    Time {
      start: 2021-06-06T22:00:00.000Z,
      reservation: [ [Reservation], [Reservation], [Reservation], [Reservation] ]
    },
    Time {
      start: 2021-06-06T22:00:00.000Z,
      reservation: [ [Reservation], [Reservation], [Reservation], [Reservation] ]
    }
  ]
  </code></pre>
  <p>I have tried it with a predefined set of times and ran them in a for-of-loop instead of the while-loop and that worked correctly so it must be the while-loop I guess.</p>
  <pre><code>const time1 = clubOpening
  let time2 = new Date(time1)
  time2.setHours(time2.getHours() + 1)
  let time3 = new Date(time2)
  time3.setHours(time3.getHours() + 1)
  
  const times = [time1, time2, time3]
  
  for (const time_ of times) { // with for-of loops for predefined times-array it works
       // code as above
  }
  
  </code></pre>