Afghanistan was different enough in that it wasn't a unified Afghan government or population fighting against a Soviet invasion in the same style of aggression of what we're seeing from Russia in Ukraine. A new local Afghan government that had recently sized power in a coup begged the Soviets to help them consolidate power in a civil war in that the Soviets knew would only escalate through international intervention in favor of their allies opposition.
Was it really a defeat for the US? Seems more like the US realized the futility of nation building there and just withdrew, hoping the propped up Afghan government wouldn't just surrender or flee.
It absolutely was a defeat, but the asymmetric numbers get worse every year for a guerilla force fighting a huge war machine. The ratio of deaths for the US versus the Afghans was staggering...the Taliban won, but at a huge cost of life.
I don't know if there's the same cultural will in Ukraine to absorb that kind of pummeling.