But non of them avoided what Allah foreordained.
But non of them avoided what Allah foreordained. Thus Imams having the knowledge of when they die is confirmed by both Sunni and Shia. It should be noted that the prophets and Imams share with the rest of humanity the means for obtaining knowledge which Allah has given: the senses, the intellect, etc. They also possess special powers and means which other people do not have.
In carrying out the commands of Allah in which people have also responsibility, and likewise in ordinary behavior, the prophets and Imams only make use of the first way of knowing, that is the commonly available means. The second means (extraordinary means) is only used by them in duties and works which are connected with their position of prophethood and Imamat.
Thus in matters such as knowing the beginning of the month, passing judgment, finding out if something is unclean or pure, etc., they make use of ordinary means such as sighting of the moon, and so forth, which anyone else employs. The extraordinary means of knowledge can not be the basis for their action (unless in some rare circumstances when Allah wishes otherwise), and what they volitionally do must be determined by the means available to everyone.
In other words, the act of Imam is based on the conclusion of pieces of evidences which can be obtained by the ordinary human beings, and not by the information related to what Allah has foreordained, otherwise a contradiction would arise: For instance, Allah foreordained that Imam Ali be killed by Ibn Muljam (LA) in the Mosque on a specific date, but since Imam Ali knows this via an extraordinary means of knowledge given by Allah, if he avoids the Mosque, he would have gone against what Allah foreordained which is impossible.
Likewise, Imam al-Husain (AS), though knew most of the people of Iraq will not follow him, he did his duty by answering their unanimous call. Imam al- Husain sent his agent, Muslim Ibn Aqil to Kufa, since he wanted to do the investigation as an ordinary human being does. This is while he knew the situation in Kufa by extra-ordinary means of knowledge and that Muslim Ibn Aqil will be killed, yet he was not supposed to act based on what is concealed from an ordinary human being.
He acts based on the extraordinary means of knowledge ONLY IF Allah wishes so, and such circumstances are rare.