Title::getArticleID() has a $flags parameter, which can be set to Title::GAID_FOR_UPDATE (or various equivalents) to make sure the up-to-date ID is actually retrieved. Presumably PageIdentity::getId should support an equivalent feature, in case the page was created just now.
What should this look like? If the $flags parameter is only used for one flag, perhaps a getIdForUpdate() method would be better than adding a parameter, but with PHP 8's named function parameters on the horizon, perhaps a boolean parameter isn't so bad.