Certainly not by walking away from him. Carter gave tacit support to the Ayatollah, and we now stare possible death of the planet in the face due to that action.
I’m not defending Carter or the Iranian Revolution, but in 1979 the Shah was dying and there was no legitimate successor to the Shah who could have held on to power.
Short of the U.S. Military occupying Iran to prop up the next Shah, thus causing nuclear war with the Soviets, there was little that could have been done.