I was reading some ReactJS code online, and I found a code snippet below.
I was wondering what is the purpose of the IIFE function inside the login
function which is executed onClick
? Can't we just make the login
async
and remove the IIFE?
const login = () => {
(async () => {
try {
//OAuth Step 1
const response = await axios({
url: `${apiPath}/twitter/oauth/request_token`,
method: 'POST'
});
const { oauth_token } = response.data;
//Oauth Step 2
window.location.href = `https://api.twitter.com/oauth/authenticate?oauth_token=${oauth_token}`;
} catch (error) {
console.error(error);
}
})();
}
This is how the login was used in the original code:
<img className='signin-btn' onClick={login} alt='Twitter login button'/>