preInit (and maybe onRuntimeInitialized?) should support Promises/async
#14,520 建立於 2021年6月23日
倉庫指標
- Star
- (27,361 star)
- PR 合併指標
- (平均合併 19天 10小時) (30 天內合併 147 個 PR)
描述
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.