When the US, after decades of engagement, leaves the Middle East (if they are foolish enough to do so) they'll be leaving a political power vacuum of Shai-majority citizens for Iran to control by proxy - which doesn't support the US ally of Israel. That, and the Taliban, the Sunni wahabists schools that enabled 9/11, are protected by the Saudis, which the US has been getting cosy with for a while. It could be argued that being friendly with Saudi Arabia by diplomatic means has been more useful in quashing the fundamentalist wahabi jahadist threat, than has been any fighting in either Iraq or Afghanistan.
that may well be the case. But it isn't a compelling reason not to leave. Our presence isn't exactly wanted by the locals, and there are plenty of other places in the world with bad things happening and proxy conflicts
I'm not of that opinion, my point is that the US military hasn't reached their 'fighting terrorism' objective, and they might have made things worse for one of their allies (this being a motivation to stay).