Three months ago, Joko Widodo, the mild-mannered and reform-minded governor of Jakarta, looked like a shoo-in to become Indonesia's next president in a crucial election that took place on Wednesday.

Yet when the unofficial "quick count" results were released, Widodo - known by his nickname Jokowi - must have thanked his lucky stars he still had a slight edge over his rival.

The immediate challenge is to get a grip on … the country’s ballooning fuel subsidy bill

While the final results of Indonesia's election won't be known until July 22, Widodo is fortunate to have retained his lead after squandering a 30-point advantage in the polls to Prabowo Subianto, a tough-talking former general who staged a remarkable comeback.

Indeed so narrow is Widodo's margin of victory - 53 per cent to 47 per cent - that Subianto's camp is so far refusing to concede defeat.

While financial markets are relieved that Widodo, whose reformist credentials and corruption-free reputation endear him to foreign investors, is still ahead, a slim victory would deprive him of a strong mandate for reform and raise serious questions about his presidency even before he takes office in October.

This is bad news for Indonesia, Southeast Asia's largest economy and the world's third-biggest democracy.

The stark contrast between the thumping victory of the popular and charismatic Narendra Modi in India's parliamentary election in May and the political polarisation in Indonesia has been thrown into sharp relief.

Markets have already taken note. Over the past three months, Indian shares have risen 14.3 per cent, while Indonesian stocks have gained just 5 per cent. The Indian rupee, moreover, has been more or less stable against the US dollar, while the Indonesian rupiah has lost 3 per cent.

While investors expected Indonesia's election to be a closely fought contest and have already turned more bullish on the country's bonds and equities after the unofficial results put Widodo in the lead, the worrying reality is that Indonesia is now a bitterly divided nation.

Not only will uncertainty about the outcome of the election persist for another 10 days or so, the prospects for much-needed fiscal and structural reform suddenly look a lot bleaker.

The domestic and external challenges confronting Indonesia's next president would be difficult enough to address in fortuitous political conditions. They will be doubly difficult to tackle when nearly half the electorate voted for the losing candidate.

The immediate challenge is to get a grip on Indonesia's public finances, in particular the country's ballooning fuel subsidy bill, which is crowding out other spending - notably on infrastructure - and is contributing to a sizeable current account deficit.

The rise in oil prices stemming from the turmoil in Iraq is increasing the subsidy bill, leading to a further deterioration in the trade deficit and putting pressure on the rupiah - already under strain because of political uncertainty.

Growth, moreover, has slowed markedly, with the economy expanding just over 5 per cent in the first quarter of this year - the slowest pace since the third quarter of 2009.

The main culprit is a sharp fall in exports, stemming mainly from a controversial ban on the export of unprocessed mineral ore introduced in January.

Indonesia's central bank - which raised interest rates by 175 basis points last year to 7.5 per cent to shore up the rupiah when Indonesia was hit hard by the sell-off in emerging markets - expects growth of just over 5 per cent this year (down from 6.5 per cent in 2011) and is unlikely to cut rates because of Indonesia's deteriorating external deficit.

Remedying all these problems won't be easy, particularly given Indonesian voters' clear preference for protectionist economic policies - strongly espoused by Subianto and favoured by Widodo to a lesser degree.

While foreign investors have been increasing their holdings of Indonesian local currency bonds this year, post-election uncertainty is testing the resilience of Indonesia's financial markets.

While Indian assets are still benefiting from "Modi mania", Indonesian ones could start to suffer much more from the conspicuous lack of "Jokowi jubilation".