0

I'm working on an Angular component. In a method in the typescript file I'm accessing an AWS S3 object with the headObject() method. In that method's callback function I'd like to be able to call the outer method again if the headObject() method fails. It seems like I should be structuring the methods using "fat arrow" operator to reference the outer method's this variable, but I can't figure out the correct way to do it. If there's a better way please point me in that direction.

getNewPass = (parameter) => {
  const bucket = new S3({
    accessKeyId: '/*************/',
    secretAccessKey: '/*************/',
    region: '/*************/'
  });

  const params = {
    Bucket: '/*************/',
    Key: /*************/
  }

  bucket.headObject(params, function (err, data) {
    if (err && err.code === 'NotFound') {
      // HERE is where the issue is
      this.getNewPass(parameter);
    } else {
      bucket.getSignedUrl('getObject', params, function(err, data) {
        if (err) {
          console.log('Error retrieving file: ', err);
          return false;
        } else {
          console.log('Successfully retrieved file.', data);
          window.location.href = data;
          return true;
        }
      });
    }
  });
}

I've blocked out some sensitive information and changed a variable name but otherwise that's what I'm working on. Thanks for the help.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
possum_pendulum
  • 159
  • 1
  • 4
  • 21

1 Answers1

0

Figured it out. I used the "fat arrow" operator to reformat the callback function into a lambda function, as exemplified here: https://stackoverflow.com/a/48808669/5943974

possum_pendulum
  • 159
  • 1
  • 4
  • 21