The foreclosure will show up on your credit even after you file bankruptcy. Late payments post-bankruptcy filing will not be reported on your credit report, but the lender will report the foreclosure sale.
Based on the facts, I would suggest the following steps in this order.
Confirm you would be liable to pay the mortgage. You need to confirm whether you live in an anti-deficiency state. This is an important first step. I would hate to see you file bankruptcy only to eliminate your liability on a mortgage when you would not be liable after the sale because of your particular state laws.
Under anti-deficiency laws, if your mortgage was for the purchase of a property you occupy or occupied as your primary residence, you will not be held responsible for any deficiency. A deficiency is the difference between what is owed on the property and what the property sells for at the foreclosure auction.
For example, if the property is worth $100,000, but you owe $150,000 on the loan, the deficiency balance would be $50,000 after the property is sold. You may be liable for that balance in your particular state. You must know whether you are liable before filing for bankruptcy.
File bankruptcy. You may be liable on the mortgage or you may have other unsecured debt (credit cards or personal loans). Make sure that you are eligible for Chapter 7 bankruptcy prior to filing. Assuming you are, then it makes sense to wipe out your liability on the mortgage and any other dischargeable debt immediately.
File for divorce. Now that you have filed bankruptcy, you have eliminated your liability on all or most of your debt. Don't agree to pay any of the joint debts that may still be in your name. The bankruptcy wiped out your responsibility to pay, so do not re-establish that liability by agreeing to pay on that debt as part of the divorce settlement agreement.
For example, you and your soon-to-be ex-husband have a joint credit card in both your names. The bankruptcy wiped out your liability, but you can re-establish that liability by agreeing to be responsible for that debt as a condition of your divorce.
This is an important distinction. If you file the divorce first and agree to be responsible for any jointly held credit cards, your bankruptcy would not eliminate your responsibility to pay on that jointly held account. Your husband could still force you to pay even after you complete the bankruptcy.
Sell the property. You can sell the property after filing for bankruptcy or divorce, and you can do this before either process is complete. I will assume you have no equity in the house since you and your husband are willing to walk away from it and allow the lender to foreclose. That likely means you will need to sell the property for less than what it is worth. This is called a "short sale." A short sale would be a lot better for your credit than a foreclosure.
Unfortunately, your husband must be willing to sign the short-sale loan documents. You can't sell it without his consent. That is why I suggest this as your last step. The bankruptcy wipes out your liability on the house, the divorce ends your marriage, and the short sale protects your credit as much as possible. The first two can be done without your husband's cooperation. The last step cannot.