title: NodeJS why won't my function be called inside my IF/Else statement
body: <p>I have a question around node and calling a function inside of if/else statements.  Below is my situation.  I'm not sure why calling the awsCreds function works from the first IF statement, but it doesn't work when I try to invoke it from the else statement.  Does it have something to do with the fact I have an if/else inside of an else statement?</p>
  <pre><code>let cacheExpiration;
  let currentTime;
  let awsCredentials;
  
  async function stsGetCallerIdentity(creds, message) {
     const stsParams = { credentials: creds };
     // Create STS service object
     const kinesis = new AWS.Kinesis(stsParams);
     const parsed_value = JSON.parse(message);
     const type = parsed_value.type;
     console.info(type);
  
     const message_type = {
        &quot;transcript&quot;: process.env.TRANSCRIPT_KINESIS,
        &quot;tone&quot;: process.env.TONE_KINESIS,
        &quot;pitch&quot;: process.env.PITCH_KINESIS
     };
     const stream_name = message_type[type];
     console.info(stream_name);
  
     const params = {
        Data: Buffer.from(message),
        PartitionKey: `1111`,
        StreamName: stream_name
     };
     kinesis.putRecord(params, function(err, data) {
        if (err) console.error(err);
        else {
           console.info(JSON.stringify(data));
        }
     });
  }
  
  function awsCreds() {
     return new Promise((resolve, reject) =&gt; {
        const roleToAssume = {
           RoleArn: process.env.IAMRole,
           RoleSessionName: `session1`,
           DurationSeconds: 900 };
  
        sts.assumeRole(roleToAssume, function(err, data) {
           if (err) {
              console.error(err, err.stack);
              reject(err.stack);
           } else {
              awsCreds = {
                 accessKeyId: data.Credentials.AccessKeyId,
                 secretAccessKey: data.Credentials.SecretAccessKey,
                 sessionToken: data.Credentials.SessionToken,
                 cacheExpiration: data.Credentials.Expiration
              };
              resolve(awsCreds);
           }
        });
     });
  };
  
  webSocketClient.on(`message`, async (message) =&gt; {
  try {
    currentTime = new Date();
    console.log(`currentTime`, currentTime);
    console.log(`cacheExpiration`, cacheExpiration);
  
  
            if (cacheExpiration == undefined) {
              //Calling awsCreds returns values.
             awsCredentials = await awsCreds();
             stsGetCallerIdentity(awsCredentials, message)
      
          } else {
             const minutes = (cacheExpiration.getTime() - currentTime.getTime());
             console.log(`minutes`, minutes);
             if (minutes &gt; 60000) {
                //Do something
             stsGetCallerIdentity(awsCredentials, message)
             } else {
                
                // Calling awsCreds from here returns error 'awsCreds is      not a function'.
  console.log(`Refreshing credentials`);
                awsCredentials = await awsCreds();
                stsGetCallerIdentity(awsCredentials, message)
             }
          }
         
       } catch (error) {
          console.error(`There was an error`, error);
       };
  )};
  </code></pre>