Melt butter in a large skillet over medium heat. Add sliced onions and cook, stirring occasionally, until deep golden and caramelized, about 25–30 minutes.
Add minced garlic and cook for 1–2 minutes until fragrant. Remove skillet from heat and set aside.
In a separate pot, cook spaghetti in salted boiling water until al dente. Reserve 1 ¼ cups of pasta water and drain spaghetti.
Whisk egg yolks with a few tablespoons of reserved pasta water in a bowl to temper.
Return skillet with onions to low heat. Add cooked spaghetti and toss with onions.
Add egg-water mixture, remaining pasta water, and olive oil. Toss quickly to coat the pasta with sauce.
Sprinkle grated Parmesan over pasta and toss until melted and smooth. Season with salt and pepper.
Plate pasta and garnish with fresh chopped parsley. Serve immediately.
Notes
Slice the onions evenly for consistent caramelizing. Sub in your favorite pasta shape if desired. Add a splash of pasta water when reheating to restore creaminess.