The official UI5 documentation proposes to load a Fragment using promises:
Fragment.load({
controller: oFragmentController,
id: oView.getId(),
name: "webapp.view.HelloDialog"
}).then((oDialog) => {
oView.addDependent(oDialog);
oDialog.open();
});
I personally would prefer to use async/await instead:
const oDialog = await Fragment.load({
controller: oFragmentController,
id: oView.getId(),
name: "webapp.view.HelloDialog"
});
oView.addDependent(oDialog);
oDialog.open();
How save would be such code refactoring and transition from .then-promises to async/await in UI5? Can it lead to some issues?
You can
safelyuseasync‑awaitin UI5since it's basically syntactic sugar for Promisewith some restrictions currently. *Given
<Dialog id="helloDialog">in the fragment definition, andthisbeing the current controller instance, creating the fragment would then look like this:With
this.loadFragment(since UI5 1.93)With
Fragment.load(since UI5 1.58)The old factory function
sap.ui.xmlfragmentis deprecated and fetches the*.fragment.xmlfile synchronously.That being said, if the app is targeting abandoned browsers such as the Internet Explorer,
async‑awaitshould be avoided completely.Promiseorthen-able functions work in IE11 only because UI5 comes with a polyfill that gets applied if thePromiseis not already natively or fully supported. *** ... if the project is based on UI5 Tooling. See the newly introduced sections "Asynchronous Factory Function" and "Event Handler Registration" from the topic ECMAScript Support in UI5.
** IE11 is no longer supported. The polyfill is also removed accordingly.