Skip to content

Commit

Permalink
⚡️ Remove async/await usage (#67)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Craig Jennings authored Feb 9, 2022
1 parent be6bc16 commit 0d64f2e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
59 changes: 30 additions & 29 deletions src/appleAuthHelpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,37 @@ const signIn = async ({
authOptions: AppleAuthOptions,
onSuccess?: Function,
onError?: Function,
}): Promise<?AppleAuthResponse> => {
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<?AppleAuthResponse> =>
/** 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,
Expand Down
11 changes: 5 additions & 6 deletions src/utils/waitForVar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

0 comments on commit 0d64f2e

Please sign in to comment.