From 1162455f3285cdb49c96836c023c2af1e7ff2167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Dyhrberg=20Thegler?= Date: Fri, 20 Dec 2019 20:54:20 +0100 Subject: [PATCH] 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. --- .../oidc/js/src/main/resources/keycloak-authz.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/adapters/oidc/js/src/main/resources/keycloak-authz.js b/adapters/oidc/js/src/main/resources/keycloak-authz.js index aa71872929..df60747641 100644 --- a/adapters/oidc/js/src/main/resources/keycloak-authz.js +++ b/adapters/oidc/js/src/main/resources/keycloak-authz.js @@ -22,6 +22,17 @@ var _instance = this; 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 () { var request = new XMLHttpRequest(); @@ -30,8 +41,10 @@ if (request.readyState == 4) { if (request.status == 200) { _instance.config = JSON.parse(request.responseText); + resolve(); } else { console.error('Could not obtain configuration from server.'); + reject(); } } }