A bankruptcy will discharge unsecured debts or judgments based on them. Therefore, whether you file bankruptcy after you’ve been sued but before you go to court, or after there’s a judgment against you, the debt will go away.
The big difference is, of course, that lawsuits can be expensive and emotionally draining. If you’re going to file bankruptcy anyway, and there’s no other reason to wait, it’s often better to just do it before you have to appear in the courtroom. That saves the worry about the lawsuit, the expense of filing a response or hiring an attorney to do so, and the indignities of putting up with the legal system.
If, on the other hand, there is a possibility you can win the lawsuit and won’t have to file bankruptcy, then take that chance. Should you go to court and lose (it happens), you can still file bankruptcy and wipe out the judgment.
Timing becomes more crucial, however, at that stage, because you don’t want the judgment creditor to be able to attach your wages, grab a bank account, or do something like that before you file.
Note that if your wages are garnished, or the like, and then you quickly file bankruptcy, you can usually recover the money. But it will take time, and will depend on what other assets you have and what other issues there are in the bankruptcy case. It’s best, as always, to consult a competent bankruptcy attorney on these issues.