From a21a94078ba855b25601fe97141b30b8b55c24c9 Mon Sep 17 00:00:00 2001 From: Ray DeCampo Date: Sun, 24 Dec 2017 11:35:11 -0500 Subject: [PATCH] KEYCLOAK-5578: Keycloak JS adapter returns native Promise instances when available. Promise instances are adorned with success() and error() functions to retain backwards compatibility. --- .../oidc/js/src/main/resources/keycloak.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/adapters/oidc/js/src/main/resources/keycloak.js b/adapters/oidc/js/src/main/resources/keycloak.js index e1cfe56bf6..37875f4339 100755 --- a/adapters/oidc/js/src/main/resources/keycloak.js +++ b/adapters/oidc/js/src/main/resources/keycloak.js @@ -772,6 +772,43 @@ } function createPromise() { + if (typeof Promise === "function") { + return createNativePromise(); + } else { + return createLegacyPromise(); + } + } + + function createNativePromise() { + // Need to create a native Promise which also preserves the + // interface of the custom promise type previously used by the API + var p = { + setSuccess: function(result) { + p.success = true; + p.resolve(result); + }, + + setError: function(result) { + p.success = false; + p.reject(result); + } + }; + p.promise = new Promise(function(resolve, reject) { + p.resolve = resolve; + p.reject = reject; + }); + p.promise.success = function(callback) { + p.promise.then(callback); + return p.promise; + } + p.promise.error = function(callback) { + p.promise.catch(callback); + return p.promise; + } + return p; + } + + function createLegacyPromise() { var p = { setSuccess: function(result) { p.success = true;