Walmart and Delta stock are both hitting all-time highs—and their CEOs have relied on a surprisingly similar strategy

In 2015, when Walmart CEO Doug McMillon was under pressure to help the discount retailer keep up with Amazon amid a period of stagnation, he made the bold decision to invest billions of dollars in raises to motivate store workers to offer better service and overhaul Walmart’s in-store tech. Wall Street’s response? A selloff that led to what was then the biggest stock drop in Walmart’s history , a 10% shellacking.

Fast-forward to this week, when Walmart’s stock hit an all-time high on the strength of a quarterly sales report and forecast that blew past Wall Street expectations. In this consumer environment, in which inflation-battered shoppers are being more careful, its U.S. comparable sales grew 5.3% last quarter. That led Walmart to raise its forecast for the year. What’s more, its e-commerce grew 27%, and in-store sales boomed, suggesting Walmart has been taking market share from the likes of Target and Amazon. Not bad for a retailer at risk of obsolescence a decade ago.

Promisingly, most of its sales growth has been coming from consumers from upper-income households, or those earning $100,000 or more per year, a cohort it has been cultivating for years with more appealing and, dare we say it, even stylish clothing brands, and a nicer presentation of groceries. To be sure, inflation has helped Walmart, which serves a more price-conscious consumer than many of its rivals. But McMillon and Walmart have also been helping their own cause. “The company hasn’t passively stood by and allowed these benefits to accrue; it has worked hard to adapt its proposition,” wrote Neil Saunders, managing director of GlobalData, in a research note.

In the years since 2015, during which Walmart recorded its first-ever revenue drop, the company tried many things to jump-start not just e-commerce but a culture of innovation that had stalled. It opened a big Silicon Valley tech lab and so-called stores of the future, and bought a bunch of brands that it later sold off again. Walmart has also proceeded gingerly but surely in pursuing new business lines, as detailed this year in a Fortune feature. But ultimately McMillon’s strategy was centered on its core business of shopping and making it more pleasant and efficient for its customers, rather than jumping on retail fads.

On a call with analysts on Tuesday, Walmart executives said the retailer’s U.S. business has had 12 consecutive months of online deliveries surpassing $2 billion, and that 30% of them are expedited orders for which consumers pay premium fees. What’s more, Walmart has begun selling items like secondhand watches on its marketplace and added new benefits to its Walmart+ loyalty program. And it now has a massive advertising business that is almost pure profit.

One weak spot remains an e-commerce business that while huge is not yet profitable. But there again, McMillon is taking the long view that has served him well in his almost 11 years at the helm. “We don’t think we should race to it. This is a long-term game,” McMillon said on an earnings call.

His counterpart at Delta Air Lines , Ed Bastian, is similarly reaping the benefits of a long-term strategy that has involved years of investment.

At Delta’s investor day on Wednesday, Bastian, CEO since 2016, said that the airline will offer more premium seats in its fleet—85% of the seats Delta plans to add in 2025 will be in this category—and that sales from those classes were expected to exceed economy tickets by the end of 2027, something that would have been unthinkable not long ago. (It is also going to be serving Shake Shack’s popular burgers on some flights.)

“We’ve been at it for 15 years, investing aggressively,” Bastian said in a briefing with reporters, referring to Delta’s efforts to make its offering more elevated with greater pricing power for the company.

Executives said at the meeting, which was broadcast on the internet, that about 15 years ago, some 12% of Delta’s domestic first-class seats were paid for, with the rest being upgrades for frequent fliers. Now, more than 70% of those first-class seats are purchased, despite intense resistance from customers for years. It has also launched a very high-end lounge called Delta One for its biggest spenders. (Delta is planning not to cede market share to deep discounters by also continuing to offer some cut-rate basic economy fares.)

To be sure, there have been missteps in building customer loyalty, such as how Delta handled changes last year that resulted in limited access to its lounges for many passengers. But on the whole, Bastian has succeeded by taking the long view on how to build business. (One example: Delta won a lot of goodwill early in the pandemic when it stopped selling middle seats longer than its rivals to make it easier for passengers to socially distance.)

Another long-term relationship that has greatly helped Delta: its co-branded credit card program with American Express , which should yield $7 billion for Delta this year and $10 billion annually long term, the airline told analysts.

Wall Street has rewarded Delta by lifting shares to all-time highs this year. (They edged down a touch on Wednesday over concerns about high expenses.)

So whenever a CEO is tempted to make a short-term move to mollify Wall Street or get a quick pop, he or she should remember that Walmart and Delta, both so-called legacy companies, are at all-time highs thanks to planning for the long term.