Q: If one of my units shoots an enemy unit in which only one
model is visible, and I resolve the attacks one at a time, slaying
that model before I have resolved all of my unit’s attacks, what
happens to the rest of the attacks – are they wasted?
A: No. You determine the number of attacks the firing
unit will make against the target unit before any of those
attacks are made (i.e. before any hit rolls are made). If
you are resolving these attacks one at a time and your
opponent removes the only visible model as a casualty,
you still continue to resolve the remainder of the firing
unit’s attacks against that target.
https://whc-cdn.games-workshop.com/wp-content/uploads/2018/09/warhammer_40000_rulebook_en.pdf
The same is true of close combat; you target a unit as a whole, determine how many attacks you are making against it, and carry out all those attacks until either you run out of attacks, or the unit runs out of models/wounds.