Behold! ye were climbing up the high ground, without even casting a side glance at any one, and the Messenger in your rear was calling you back. There did Allah give you one distress after another by way of requital, to teach you not to grieve for (the booty) that had escaped you and for (the ill) that had befallen you. For Allah is well aware of all that ye do.
In order to understand the proper sequence of this verse, we need to recast another glance over the previous verse, verse 152, that states “thumma ṣarafakum ‘anhum li-yabtaliyakum (then Allah did divert you from your foes in order to test you)”. In other words, because of their disobedience and lapses, Allah made them taste defeat in order to test them. Furthermore, the verse makes it clear that instead of punishing them for their disobedience, Allah chose to put them through a test so as to forgive them and to bestow His bounty and grace upon them. Following this, the words “idh tuṣ‘idāna (remember, when you were fleeing) to “fa athābakum ghammam bi ghammin …( there did Allah give you one distress after another”) gives the nature and detail of this trial. And the words “li kay la tahzanū ‘ālā mā fātakum wa la mā aṣābakum (to teach you not to lose heart or to grieve for the loss suffered or the ill that had befallen you)” underline the lesson that the believers could learn and benefit from in the light of this experience, provided they faithfully and sincerely follow the teachings of Islam and persevere in its cause.
Islahi, Amin Ahsan. Pondering Over The Qur’an: Surah Ali Imran (pp. 270-271). Islamic Book Trust. Kindle Edition.