A VERY close run thing. Best book on this is “Shattered Sword.” These guys write like they served in the IJN. A perfect (for us) combination of bad decisions by Nagumo, timing (for different waves of our attackers), and luck (the scout plane not seeing the US carrier fleet).
As with Pearl Harbor, the Midway operation depended on surprise.
Operation K failed to ascertain the location of our carriers. Combined with much heavier than usual American radio traffic and aggressive PBY air patrols, I would place the bulk of the blame w/Yamamoto for not accepting that the all-important element of surprise was not likely.
Of course, the loss of face for retreating before joining battle was not going to happen.
I’ll check out Shattered Sword.