Iyov’s friends asked him, Did you arrange your prayer while you were still free of distress? (Iyov 39:19). Based on this Rabi Eliezer said: A person should always pray before troubles befall him, for if Avraham had not davened [for his descendants] when he was between Beis El and Ai, none of Am Yisrael would survived the battle [for Ai].
Reish Lakish said: If someone exerts himself in prayer on earth, he will have no enemies in Heaven Above. He explains the pasuk cited above as indicating that if Iyov would have cried out to Hashem in advance, he would have been spared all his suffering.
Rabi Yochanan said: A person should always pray that the angels on High will always help him and that he will not have enemies from Above. He interpreted the pasuk as saying that if Iyov had cried out to Hashem, pleading that no prosecutors should act against him, and if he had requested that the angels on High would come to his aid, he would have avoided his suffering. (Maseches Sanhedrin 44b)