From 0d64f2e3087140b79dcf07ad81d657bb9f24e7f8 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Wed, 9 Feb 2022 17:56:09 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Remove=20async/await=20usa?= =?UTF-8?q?ge=20(#67)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove async/await usage This resolves the issue where consumers of this library are expected to include 'regenerator-runtime/runtime' even if they only support browsers that have async/await support. * 1.0.5 --- package.json | 2 +- src/appleAuthHelpers/index.js | 59 ++++++++++++++++++----------------- src/utils/waitForVar.js | 11 +++---- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 633800c..f3b1811 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-apple-signin-auth", - "version": "1.0.4", + "version": "1.0.5", "description": " Apple signin for React using the official Apple JS SDK", "author": { "name": "Ahmed Tarek", diff --git a/src/appleAuthHelpers/index.js b/src/appleAuthHelpers/index.js index 0efe3d7..20ca956 100644 --- a/src/appleAuthHelpers/index.js +++ b/src/appleAuthHelpers/index.js @@ -17,36 +17,37 @@ const signIn = async ({ authOptions: AppleAuthOptions, onSuccess?: Function, onError?: Function, -}): Promise => { - try { - /** wait for apple sript to load */ - await waitForVar('AppleID'); - /** Handle if appleID script was not loaded -- log + throw error to be caught below */ - if (!window.AppleID) { - console.error(new Error('Error loading apple script')); - } - /** Init apple auth */ - window.AppleID.auth.init(authOptions); - /** Signin to appleID */ - const response = await window.AppleID.auth.signIn(); - /** This is only called in case usePopup is true */ - if (onSuccess) { - onSuccess(response); - } - /** resolve with the reponse */ - return response; - } catch (err) { - if (onError) { - /** Call onError catching the error */ - onError(err); - } else { - /** Log the error to help debug */ - console.error(err); - } - } - return null; -}; +}): Promise => + /** wait for apple sript to load */ + waitForVar('AppleID') + .then(() => { + /** Handle if appleID script was not loaded -- log + throw error to be caught below */ + if (!window.AppleID) { + console.error(new Error('Error loading apple script')); + } + /** Init apple auth */ + window.AppleID.auth.init(authOptions); + /** Signin to appleID */ + window.AppleID.auth.signIn().then((response) => { + /** This is only called in case usePopup is true */ + if (onSuccess) { + onSuccess(response); + } + /** resolve with the reponse */ + return response; + }); + }) + .catch((err) => { + if (onError) { + /** Call onError catching the error */ + onError(err); + } else { + /** Log the error to help debug */ + console.error(err); + } + return null; + }); export default { APPLE_SCRIPT_SRC, signIn, diff --git a/src/utils/waitForVar.js b/src/utils/waitForVar.js index 2ba0db9..eac4551 100644 --- a/src/utils/waitForVar.js +++ b/src/utils/waitForVar.js @@ -8,7 +8,7 @@ * - eg: `waitForVar('FB', { pollFrequency: 500, retries: 2, parent: window || global }).then(FB => FB.test())` * - eg: `waitForVar('FB', { retries: ({ retries } => retries * 500) }).then(FB => FB.test())` */ -const waitForVar = async ( +const waitForVar = ( name, { pollFrequency = 1000, @@ -22,21 +22,20 @@ const waitForVar = async ( ) => { // eslint-disable-next-line no-prototype-builtins if (parent && parent.hasOwnProperty(name)) { - return parent[name]; + return Promise.resolve(parent[name]); } if (!inRetries) { - return undefined; + return Promise.resolve(undefined); } const retries = inRetries - 1; - await new Promise((resolve) => + return new Promise((resolve) => setTimeout( resolve, typeof pollFrequency === 'function' ? pollFrequency({ retries }) : pollFrequency, ), - ); - return waitForVar(name, { pollFrequency, parent, retries }); + ).then(() => waitForVar(name, { pollFrequency, parent, retries })); }; export default waitForVar;