By John Simpson
BBC News Foreign Affairs editor, Tehran
This was not, of course, the result the West was hoping for. But political chaos and public disorder in Iran is not what any outside government wants either.
The election choice was basically between the openly anti-Western Mahmoud Ahmadinejad, and Mir-Hossein Mousavi, who made it clear he wanted to end Iran's isolation and talk to the Americans.
It is certainly is not in the outside world's interest to have a long period of disorder in Iran
It would certainly not have been an easy relationship, even if Mr Mousavi had become president. Iran will always be a difficult country for the United States, Britain and other Western countries to deal with.
Even the Shah, who lost his throne because he tried to westernise Iran too quickly, was a difficult man to do business with.
Iran is an important regional power with an historical sense of having been held back by the West; its interests are bound to clash with those of the West from time to time.
As far as the West was concerned, the choice between Mr Ahmadinejad and Mr Mousavi was between grades of difficulty.
Now the question has been settled. But for the outside world in general, it has happened in a way which will affect Mr Ahmadinejad's entire new four-year term as president.
According to an Iranian newspaper on the day of the election, a government minister in Indonesia hailed Iran as an example to the world of democracy in action.
After the riots in the streets and the clampdown on leading reformist politicians, we are unlikely to hear much of that kind of sentiment for a while.
But it certainly is not in the outside world's interest to have a long period of disorder in Iran. Political chaos in a leading oil-producing country would do more economic damage to Western countries.
They will surely prefer President Ahmadinejad, with his reputation tarnished, to that.
The aftermath of the election has shown one important difference from the past: it is harder than ever for the authorities in a relatively sophisticated country like Iran to clamp down on dissent.
Everything now depends on whether Mr Ahmadinejad can quieten things down without creating more anger on the streets
Reports here say that the bureau of the respected Al-Arabiya 24-hour news station has been closed.
Action has been taken against other foreign journalists. BBC Online has been blocked from time to time, and so have mobile phone services.
Yet people right across the country have been kept fully informed of what is going on - there are so many ways people can get the news nowadays.
Realising this, even Iran's own state broadcaster (IRIB) has been showing pictures of the worst of Saturday's rioting in Tehran, a decision that may well have been taken at a very high level.
The Islamic republic has reached a difficult moment in its history.
Everything now depends on whether Mr Ahmadinejad can quieten things down without creating more anger on the streets.
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.