preInit (and maybe onRuntimeInitialized?) should support Promises/async
#14.520 aperta il 23 giu 2021
Metriche repository
- Star
- (27.361 star)
- Metriche merge PR
- (Merge medio 19g 10h) (147 PR mergiate in 30 g)
Descrizione
Because of how async works in JS (your function just starts returning a Promise) it's easy for someone to make a preInit or onRuntimeInitialized handler async and not notice that the behavior is subtly wrong, introducing a race condition. Handling this by hand using runtime dependencies is a little tricky because of how promise fulfillment works (next event loop turn) vs how runtime dependencies work (as soon as they're all gone, the callback is invoked synchronously), so you can accidentally introduce a new race when doing it. If preInit (and potentially onRuntimeInitialized) were updated to notice if the handler returned a thenable it would be possible for all this to just work and it would reduce the risk of people adding races by misusing runtime dependencies or forgetting about them entirely.
I think implementing this would just mean changing the loop at the bottom of generated JS that runs preInit functions so that it notices thenables and chains off .then if it encounters one, and then do the final run invoke in the last callback.