Promises in application.run do not fullfil until loop exit
Follow up of a conversation on the GNOME JavaScript room https://matrix.to/#/!pOGgkAueAsusiyFCTb:matrix.org/$1645999262218956tohmj:matrix.org?via=gnome.org&via=matrix.org&via=privacytools.io
Where using a dynamic import to load the application, promises do not fulfill until after the loop exit.
The GNOME Builder - JavaScript - GNOME Application template suffers from the issue, any promise will not fullfill until after the application exit.
Here is a simpler reproducer
// entry.js
// ko
import("./main.js").then((main) => main.default())
// ok
// import main from './main.js'
// main();
// main.js
import GLib from "gi://GLib";
import Gtk from "gi://Gtk";
export default function main() {
const application = new Gtk.Application({
application_id: "what.ev.er",
});
application.connect("activate", () => {
const window = new Gtk.ApplicationWindow({application})
Promise.resolve().then(() => {
log('2')
});
window.present();
});
log("1");
application.run([]);
log("3");
}
and run with
flatpak run --command="gjs" --socket=session-bus --filesystem=home --socket=wayland org.gnome.Platform//master -m entry.js
Expected result:
1
2
3
Actual result:
1
3
2
Reproduced with gjs 1.71.1 (org.gnome.Platform//42beta) and gjs 1.71.90 (org.gnome.Platform//master).
UPDATE: here is a temporary solution:
// entry.js
import GLib from "gi://GLib";
import { exit, programInvocationName } from "system";
const loop = new GLib.MainLoop(null, false);
import(`./main.js`).then(({ default: main }) => {
// Temporary solution to https://gitlab.gnome.org/GNOME/gjs/-/issues/468
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => {
loop.quit();
const exit_code = main([programInvocationName, ...ARGV]);
exit(exit_code);
return GLib.SOURCE_REMOVE;
});
});
loop.run();
Edited by Sonny Piers