KEYCLOAK-10894 Adds a ready indicating promise

This is non-intrusive and backwards compatible. With this change it is possible
to `await keycloakAuthorization.ready` to make sure the component has been
properly initialized.
This commit is contained in:
Asbjørn Dyhrberg Thegler 2019-12-20 20:54:20 +01:00 committed by Pedro Igor
parent 0219d62f09
commit 1162455f32

View file

@ -22,6 +22,17 @@
var _instance = this; var _instance = this;
this.rpt = null; this.rpt = null;
var resolve = function () {};
var reject = function () {};
// detects if browser supports promises
if (typeof Promise !== "undefined" && Promise.toString().indexOf("[native code]") !== -1) {
this.ready = new Promise(function (res, rej) {
resolve = res;
reject = rej;
});
}
this.init = function () { this.init = function () {
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
@ -30,8 +41,10 @@
if (request.readyState == 4) { if (request.readyState == 4) {
if (request.status == 200) { if (request.status == 200) {
_instance.config = JSON.parse(request.responseText); _instance.config = JSON.parse(request.responseText);
resolve();
} else { } else {
console.error('Could not obtain configuration from server.'); console.error('Could not obtain configuration from server.');
reject();
} }
} }
} }