Struggling to understand optimism with the belief in divine decree

Whenever I try to stay optimistic or hopeful, whether about things working out one way or another, my mind often says, "Yeah, it's going to be fine." For example, when I'm applying for a job, hoping to perform well in a test, or expecting to stand out among my peers in a competition, I try to picture myself succeeding. But then, a thought crosses my mind: "This world, sometimes you get what you want, sometimes you don’t."

According to Islam, Allah (SWT) gives to whom He wills. To say it again, this world is for whomever Allah chooses; not everyone gets everything they desire. So, my mind shifts from optimism to a more resigned belief, it may happen, or it may not. That’s how I understand life; things happen only if Allah wills it.

When I think about it this way, I start wondering: What if it doesn’t happen? And then my mind spirals into worst-case scenarios. I begin ruminating, imagining all the things that could go wrong. It becomes an endless loop, a cycle of panic.

When I try to be hopeful again, another voice in my head says, "It will only happen if Allah wills it." There’s no guarantee, and sometimes Allah may not grant it because of past sins.

If anyone could explain this feeling or offer some insight into this conflict of hope and acceptance, I’d really appreciate it.