preInit (and maybe onRuntimeInitialized?) should support Promises/async
#14.520 aberto em 23 de jun. de 2021
Métricas do repositório
- Stars
- (27.361 stars)
- Métricas de merge de PR
- (Mesclagem média 19d 10h) (147 fundiu PRs em 30d)
Description
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.