Tesla earned a profit in the third quarter, fulfilling Elon Musk’s promise of a turn to positive cash flow amid months of scandals involving its high-profile CEO and federal probes, the company announced Wednesday.

The electric-car giant’s deliveries of its Model 3 sedan grew to 56,000 as America’s most valuable automaker surmounted production delays, delivery woes and a federal regulator’s lawsuit over Musk’s tweets.

Tesla logged a record $312 million in profit, swinging into the black for the first time since late 2016. It had $6.8 billion in revenue, compared with analysts' estimates of roughly $6.3 billion, and more than doubled its revenue compared with the same quarter last year. Tesla’s share price jumped 12 percent in after-hours trading.

The $50 billion car company, Silicon Valley’s biggest challenger to Detroit, has won fervent loyalty from buyers of its sleek, pricey, battery-powered SUVs and sedans. But it has scrambled for years to prove it can survive in a hotly competitive industry packed with cheaper and more traditional gas-powered cars.

“Sufficient Model 3 profitability was critical to make our business sustainable – something many argued would be impossible to achieve,” Musk wrote in a letter to investors Wednesday.

Musk, who had pledged that a ramp-up of Model 3 production would help turn the company profitable in the second half of the year and squash the need for the company to raise more money, heralded the results as “a truly historic quarter.” The company said “less than 20 percent” of its roughly 450,000 reservations for the Model 3 had been cancelled and called it “a truly mainstream product.”

A call with analysts to announce the results Wednesday evening — during which Musk has in the past tussled with analysts for asking “boring, bonehead” questions — was unconventionally low-key, possibly a response to investors' calls for the company to act more predictably and for Musk to be more “grown up.”

Musk and other executives said they had made progress on self-driving technology and factory safety. Musk also gave effusive thanks to Tesla’s workers and a group of volunteers who helped the company deliver cars at the end of the quarter: “Really chokes me up,” he said.

The company also said it would accelerate its manufacturing timeline in China, where it has pushed to open a factory in Shanghai that could make Model 3s for Chinese buyers as soon as 2019. But tariffs later this year on parts made in China, the company said, could shave roughly $50 million off its gross profit in the fourth quarter.

Tesla’s stock price this year has traced a volatile path amid growing scrutiny of Musk, an eccentric billionaire who has in recent months smoked pot during a live-streamed interview and lampooned critics, investors, journalists and federal regulators. The company’s shares have lost roughly a quarter of their value since their August peak.

But the profitable results will likely supercharge investor interest and give Musk broader leeway to shape the company as he sees fit. The company’s cash stockpile, which grew from $2.2 billion to about $3 billion, could also ease pressure on the company to raise cash from investors who could help pay for new investments and outstanding debts. Musk said the company, which is making $75 million of products per day, did not intend to raise more equity or debt.

Tesla’s growth has converted even some of the short sellers who Musk has criticized as conspiring to destroy his company. Andrew Left — a longtime Tesla short seller who is currently suing Musk and the company over the take-private tweets, which he called “a textbook case of fraud” — issued a stunning reversal Tuesday when he said he now believed the automaker was “destroying the competition.”

“The Model 3 is a proven hit and many of the (Tesla) warning signs have proven not to be significant,” Left wrote in a blog post Tuesday. “While everyone is focused on Elon smoking weed, he is quietly smoking the whole automotive industry.” Left’s lawsuit against Tesla is ongoing.

Tesla critics have argued the company faces dire threats from the growing pack of sleek electric cars, many of them sold by mass-market and luxury automakers with far more robust national networks of dealerships and repair garages.

But Tesla’s Model 3 has so far trounced the sales of rival luxury cars from BMW, Lexus and Mercedes-Benz. Boosted by Musk’s celebrity, the cars’ unconventional styling and high-rated technical features have also persuaded a growing number of buyers to join the Tesla fold.

Musk and Tesla agreed last month to pay $40 million to settle a Securities and Exchange Commission lawsuit charging Musk with lying to investors when he pledged he had the funding to take Tesla private. Musk also agreed to step down as chairman for three years but will remain chief executive. His replacement has yet to be named.

The company has pushed to pivot away from the settlement and return to normal business, including with a surprise announcement last week that the company would sell the Model 3 for about $46,000 — a discount from previous versions, but still more expensive than the $35,000 version Musk had promised would herald the company’s transformation from a luxury automaker into a company with mass-market clientele.

Musk said a $35,000 Model 3 may be available in less than six months. “Our goal is to make electric cars everyone can afford,” Musk said. “If we could produce the $35,000 car today, we would do it. There’s more to do ... but that’s our mission.”

He also hinted at projects under long-term production, including its Model Y crossover vehicle, a Tesla semi-trailer and a Tesla pick-up truck, which he called “next-level stuff.” “We’ve got the most exciting product roadmap of any company on earth,” he said.

The company has stumbled, however, in delivering on some of its most fundamental promises, including the rollout of “Autopilot” technology that would help make Tesla a leader in self-driving cars. The company last week quietly removed a pre-order option for its “full self-driving capability” mode due to what Musk called buyer “confusion,” and analysts now believe the long-promised technical breakthrough may still be months or years away.

Selling the cars is just the beginning, and critics point to a series of pitfalls along Tesla’s path to long-term survival. The company has roughly $3 billion in cash and more than $10 billion in debts due within the coming months.

JMP Securities analyst Joseph Osha said Wednesday before the new financial results that Tesla could likely survive another 18 months without having to raise money but suggested the company would be “well served” by another several billion dollars from investors or financiers. “There is little room for error,” he wrote.

Tesla buyers have also struggled to keep the cars in top shape and on the road, a problem exacerbated by the company’s manufacturing delays and its limited collection of nationwide repair centers. In a new Consumer Reports annual survey released Wednesday, based on hundreds of thousands of driver reviews, Tesla’s reliability rating plunged six spots to 27th out of 29 automakers.

Tesla’s longest-selling car, the Model S, also lost its Consumer Reports recommendation due to widespread suspension issues; a Tesla spokesperson said the issue has been addressed in recent models. Tesla’s Model X, an SUV that sells for between $80,000 and $140,000, was also ranked among the “least reliable cars” on the market due to problems including with its upward-folding “falcon wing” doors.

Musk agreed to step down as Tesla’s chairman for three years as part of a settlement with the SEC, whose investigators charged him with lying to investors. Another investigation by the Department of Justice is ongoing. The company agreed to more closely monitor Musk’s public statements, including the often-outlandish tweets he fires off to his 23 million followers.

The company has not named a new leader to replace Musk on the board, and it has not explained how it will keep a closer eye on the tweets coming from Musk’s cellphone. Musk declined to answer a question about Tesla’s next chairman, saying the questions were restricted to “operational” topics.