In the late 1980s, a U.S. diplomat in Riyadh went to ask a small favor from then Crown Prince Abdullah bin Abdulazis, who replied: A friend who doesn’t help you is no better than an enemy who does you no harm.

For much of the Obama presidency, however, Saudi Arabia has wondered whether the United States had fallen into the category of an unhelpful friend.

Diplomats say that King Abdullah, who died Thursday, had been angry in recent years at President Obama’s failure to topple Syrian leader Bashar al-Assad, was disappointed with the lack of U.S. pressure for an Israeli-Palestinian settlement, and anxious about whether U.S.-led nuclear talks with Iran would lead to a rapprochement between the United States and Saudi Arabia’s main rival.

But U.S. officials say that over the past few months, ties between Washington and Riyadh have warmed again, bolstered by an Obama visit to the Saudi capital last March and, more importantly, by both countries’ overriding focus on blunting the rise of the Islamic State, also known as ISIL or ISIS.

On Tuesday, Obama will stop in Riyadh again, this time on his way back from a trip to India, to pay his respects and firm up ties to the new king, Salman bin Abdulaziz.

King Salman takes leadership of Saudi Arabia after his brother, King Abdullah, died on Friday. The new Saudi ruler quickly named the top tier of the Saudi hierarchy. (Reuters)

“The relationship is on a sound track,” said a senior administration official. “I don’t want to paint a picture of complete harmony. They want us to be more aggressive on Iran. They want us to be more aggressive in Syria,” he said. But, he added, “I think on both sides there has been an evolution. I think the threat of ISIL has certainly brought the two countries together.”

He said that “the Saudis see ISIL as a direct threat to their internal stability” and that during a December visit by Prince Mohammed bin Nayef, the Saudi internal security chief, “there was no obvious difference of views” and “on issue after issue we have been able to agree on a way forward.”

The stakes in the relationship are high. The United States needs help from Saudi Arabia, home of the extremist Wahabi strain of Islam, in tracking down terrorists and choking off funding to militant jihadi groups. And as the world’s largest crude oil exporter, Saudi Arabia’s stability is critical for the world economy. Saudi Arabia, for its part, needs U.S. help to protect its vast oil infrastructure and shipping lanes for its oil tankers.

Recently, Saudi fighter jets have taken part in bombing raids against Islamic State forces. And the government has hunted down returning Islamic State warriors, while preaching a more moderate brand of Islam to head off recruiting efforts by extremist groups.

“I think the Saudis have started to see ISIL and the jihadi blowback as increasingly threatening to them,” said F. Gregory Gause, head of the international affairs department at the Bush School of Government and Public Service at Texas A&M University. “That has mitigated their enthusiasm for the Syrian revolt. Now much more attention is being paid to inoculating the home front and criminalizing those aiding the jihad.”

The emergence of a new common foe, however, might not be the best way to rebuild a relationship, especially since the foe has emerged from Iraq. Abdullah had opposed the U.S. invasion of Iraq, but when President George W. Bush asked him to let U.S. forces use northern Saudi Arabia as a staging area Abdullah granted the favor.

Since then, say some former American diplomats, the relationship with the kingdom has changed, from one in which favors, mutual understanding and personal ties play important roles, to one comprised of a litany of case-by-case requests, so that the Obama administration can in the space of a month, host a leading Saudi prince, condemn a public lashing and then praise Saudi cooperation on fighting the Islamic State.

“Now everything is transactional,” said Chas W. Freeman, a veteran diplomat who was U.S. ambassador to Saudi Arabia under President George H.W. Bush. “The Saudis now ask: What’s in it for us? Everything is one-off. There is a fundamental shift in the relationship.”

That relationship dates back to Feb. 14, 1945, when President Franklin D. Roosevelt met for five hours with King Abdul Aziz ibn Saud on the deck of a U.S. destroyer in Egypt’s Great Bitter Lake to plot out a post-war order for the kingdom, where an American company had discovered oil in 1938.

By the 1970s, President Richard M. Nixon saw Saudi Arabia and Iran, then ruled by the shah, as “twin pillars” of regional stability and bulwarks against Soviet influence in the region during the Cold War. After the Soviet invasion of Afghanistan and the fall of the shah, President Jimmy Carter vowed to do whatever was necessary to protect Saudi Arabia and the flow of oil through the Strait of Hormuz.

The participation of Saudi citizens in the terrorist attacks on Sept. 11, 2001, and the flow of Saudi charity money to groups like al-Qaeda turned many Americans against the kingdom, but relations between the two governments remained strong, if often troubled.

For most of the Obama presidency, U.S.-Saudi relations have been marked by tension, having mostly to do with Obama’s handling of the Syrian civil war.

Abdullah was angry that Obama didn’t do more to overthrow Assad, or punish the Syrian regime for using chemical weapons in the conflict, or give guns to the Syrian rebel groups.

“The irony is that the genuine, legitimate and moderate opposition — which Saudi Arabia would like to see supplied with defensive weapons which the West continues to deny them — is now fighting both Assad’s forces and their allies from Shia forces on one side and the so-called Sunni jihadists al-Qaeda fighters and other bloodthirsty irregulars on the other,” Prince Turki al Faisal said in a London speech in May last year. “This is a shameful situation and a dark blot on the moral standing of the world.”

In an earlier speech, Turki had said “public opinion in the kingdom, and I think in all of the Muslim world, is very much disappointed in the way that the United Sates has dealt with this issue, along with the Palestinian issue.”

Abdullah also broke openly with the United States over Egypt, deploring the lack of U.S. support for the Mubarak regime. Later, when the Obama administration cut aid to Egypt, Saudi Arabia pledged to provide the lion’s share of a $12 billion gulf aid package to help new Egyptian leader Gen. Abdel Fatah al-Sissi, who had ousted an elected Muslim Brotherhood government.

Obama’s March 28 visit came amid these tensions. “It was less intended to accomplish something than to preclude bad things from happening,” said Freeman, “and I think it worked on that level. There had been a whole series of Saudi statements criticizing the United States. That is not the Saudi style that we know. And it reflected genuine exasperation.”

Saudi Arabia has also feared that a deal on Iran’s nuclear program could lead to a warming of U.S. relations with its rival Iran, and that prolonged negotiations might sap U.S. desire to confront Iran as it expands its influence in Beirut, Baghdad, Damascus and Sanaa. The collapse of the government in Yemen this week at the hands of groups backed by Iran will only heighten Saudi concerns about the stability of its southern border.

“They do raise the fact that we need to be more aggressive about countering Iran’s threat in the region, and they wonder whether nuclear talks constrain our ability to counter Iran’s efforts in region,” said the senior U.S. official. “Our answer is, ‘no.’ We can compartmentalize talks. And a nuclear-armed Iran is more dangerous to Saudi Arabia than any agreement we might reach.”

Opposed to Bush’s decision to invade Iraq, Abdullah was also worried about Obama’s decision to withdraw, which reinforced fears that the United States, flush with new oil supplies and talking about a foreign policy and military “pivot” to Asia, would abandon the Persian Gulf.

“I think we always tend to be a bit in crisis mode about the Saudis,” said Gause. “This whole idea about America leaving the Middle East is an exaggeration but is very intensely felt in the gulf states because they rely on us for military protection.”

Where will relations go after Abdullah? Though Abdullah introduced some progressive reforms in Saudi Arabia, the unequal treatment of women and suppression of dissent creates a certain distance from the United States. Human Rights Watch noted last August, the same month that American Jim Foley was beheaded by Islamic State militants, that 19 people were beheaded in Saudi Arabia, eight of them for nonviolent offenses such as drug smuggling.

But the cultural gap is narrowing. Many members of the next generation of Saudi leaders studied in the United States, where there are currently about 54,000 Saudis studying. Gause said that the new generation “learned at their fathers’ knees that America was their big relationship.”

Gause added that “the old Cold War solidarity” has disappeared, but he said that “interests will keep the two states together.”