There are three distances at which you can deal with an attack. For want of more precise terminology these are: before contact, at contact and after contact.
Let me explain these distances better. If you want they are also temporal as well as physical.
The attacker is moving toward you to either hit or grab. They are just entering your personal space – that zone around you where you become vulnerable to attack.
The attacker has entered you personal space and has almost touched you but maybe not quite; they have reached out to grab you or their punch has been thrown.
The person has got you – either with a strike (ouch) or they have grabbed you with a strong hold.
Each of these distances represents diminishing levels of freedom on your part. At the first distance you have the most freedom to deal with the situation. At the last distance you have the least (and may well have been knocked out!)
The distance at which you deal with an attack depends upon, how aware you are to the situation, what your reflexes are like, whether you are constrained (physically or psychologically), how subtle and fast they are.
In any case the best position to be in is the first distance. Well aware of the attack and with the most options available. In this situation you can attack the attacker (preemptively) or make your escape or perhaps even lead the attacker into a position that is unfavourable to them (the most aiki approach).
In the last position, particularly from a grab, you have limited freedom of movement and have basically two approaches. One is to create more freedom using atemi (strikes). The strikes in these situations are not designed, necessarily to disable the attacker but to disrupt them enough to give you options to escape or take control of the situation. The other approach is to move. First you should move things that are not constrained. If your wrist is held then you can move just about everything else other than you wrist. Leave your wrist in the position that it is in and move your whole body around it. This is a form of blending and is fundamental to aikido. This movement allows things to develop and creates opportunities to break the attackers balance and execute an aikido technique.
In the middle distance you are not able to preempt the attack but can still move in a way to lead the attacker into a position where you can control them. This is the most common aikido situation. Timing and blending exercises are important to develop the sensitivity required to achieve this. This is sometimes described as ki no nagare.
Whenever you are practising an aikido technique you should consider at what distance it is most suited and how you could adapt it to the three different distances.