ALBANY — There were two special legislative elections in New York on Tuesday, both won by Democrats. The outcome of one was a foregone conclusion, the other an utter surprise — especially given the convincing margin of victory by the Democrat, Christine Pellegrino.

Ms. Pellegrino defeated her Republican opponent, Tom Gargiulo, in the Ninth Assembly District on Long Island, 58 percent to 42 percent, even though the district is heavily Republican and President Trump won it with 60 percent of the vote.

Political observers are paying close attention to special elections across the nation this year as potential harbingers of what may happen in the midterm contests in 2018. Ms. Pellegrino’s success appears to have drawn that kind of attention, at least in New York.

“This sends a strong message to Democrats and Republicans,” said Bill Lipton, the director of the New York Working Families Party, which endorsed her. “To Democrats, it says that they can win in Trump country when they support bold, populist champions who put working people’s issues front and center.