Tesla said Wednesday that its lone carmaking factory in Fremont, Calif., had made 5,000 Model 3 sedans a week multiple times during July and should be able to continue at that rate through the third quarter. (Mason Trinca for The Washington Post)

Tesla said Wednesday that it had burned less cash than expected during the second quarter and was nearing profitability, a long-doubted goal for the beleaguered automaker racing to boost production of its all-electric cars.

The automaker said that it had made $4 billion in car, battery and leasing revenue during the second quarter of the year, a sharp increase over the same quarter last year because it had finally introduced its newest Model 3 sedan. The stock climbed about 4 percent in after-hours trading.

Tesla’s second-quarter losses of $743 million were lower than analysts’ estimates, of about $900 million. But its cash burn extended a money-losing streak for chief executive Elon Musk during one of the company’s most challenging years in history.

The results marked a critical moment for a $50 billion company long believed to be on the brink. It was the last financial update before the second half of the year, when Musk had promised the company would be profitable.

“It took 15 years to execute on our initial goal to produce an affordable, long-range electric vehicle that can also be highly profitable,” Musk wrote in a shareholder letter Wednesday. “In the second half of 2018, we expect, for the first time in our history, to become both sustainably profitable and cash flow positive.”

Musk tweeted in April that the company’s profitability would mean it would have “no need to raise money,” but the company has billions of dollars in debt already, including more than $1 billion that will need to be paid or refinanced within the next year.

The earnings announcement and a following call with investment analysts have been widely anticipated by Tesla boosters and critics for signs of the firm’s performance — and for how the outspoken Musk would react. In May, during the company’s first-quarter earnings call, Musk berated analysts for asking what he said were “boring, bonehead” questions.

But Musk proved far more subdued during Wednesday’s call, apologizing to analysts he had clashed with during the last call for being “impolite.” “No excuse for bad manners,” Musk said, adding that at the time he was working overly long hours on “no sleep.”

Tesla is America’s youngest major automaker and has at times been valued more than General Motors and the other Detroit auto giants it has pledged to disrupt. But it is also the most heavily shorted company on Wall Street, with an army of investors betting billions that the cash-burning company’s turbulence would ensure its imminent collapse.

The company’s cash reserves have dropped to $2.2 billion, down from $3.3 billion at the end of last year. In June, Musk said the company would reduce costs by laying off 9 percent of its workforce. Tesla’s stock has fallen more than 18 percent since its June peak.

Musk reiterated that the company would “not be raising any equity at any point.” “Are we running low on money? The answer is no,” Musk said.

Tesla clinched a long-delayed milestone in late June when it announced it had built more than 5,000 of its Model 3 sedans in a week — a pace Musk has said would quickly shepherd the company into profitability. In celebrating that goal, Musk upped the ante, saying the company would produce 6,000 a week by the end of August.

Tesla said Wednesday that its lone carmaking factory in Fremont, Calif., had made 5,000 Model 3 sedans a week multiple times during July and should be able to continue at that rate through the third quarter.

“We aim to increase production to 10,000 Model 3s per week as fast as we can,” Musk wrote in the Wednesday letter, though he offered no estimated timeline. “It’s fair to say that no production ramp of any other product has been as closely watched and debated as that of Model 3.”

The company has struggled to deliver the Model 3, which critics have celebrated as a “modern marvel,” at a price cheap enough to win over new buyers who have been priced out of Tesla’s luxury line. Though promised as a $35,000 mass-market car, the only models available now sell for about $50,000 or more.

Tesla also faces broader challenges that could make its cars more difficult to sell. The company’s sale of its 200,000th vehicle last month triggered the phaseout period for a $7,500-per-car federal tax credit granted by the Obama administration to encourage the sale of electric cars. That credit will be halved for Tesla buyers next year and dwindle down to zero in the following months.

Musk himself has garnered headlines in recent months for all the wrong reasons, including after calling a hero of the Thai cave rescue a “pedo.” Musk later apologized.

The rocky results Wednesday will probably encourage Tesla skeptics who have criticized the company as underdelivering on its promise of reshaping the auto industry. In a letter Tuesday to clients, billionaire hedge-fund manager David Einhorn criticized Tesla’s manufacturing sprint and questioned “whether customers will be happy with the quality of a car rushed through production to prove a point to short sellers.”

Einhorn, a short seller who had bet against Tesla’s stock, also said he would not renew his own lease on a Model S, blaming touch-screen and window problems. Musk responded to the news on Twitter early Wednesday by saying he would send Einhorn “a box of short shorts to comfort him through this difficult time.”