0

I have this piece of code:

const AWS = require('aws-sdk');

async function getAWSKeys(){
  AWS.config.update({ region: 'us-west-2' });
  const SSM = new AWS.SSM();
  
  let ssAccessKeyPath = '/ss/s3/aws_access_key_id';
  let ssSecretKeyPath = '/ss/s3/aws_secret_access_key';
  
  console.log('before');
  const params = await SSM.getParameters({Names: [ssSecretKeyPath, ssAccessKeyPath], WithDecryption: true}).promise();
  console.log('after');
  return params;
}

var parameters = getAWSKeys();
console.log('1');
console.log('2');
console.log('3');
console.log(parameters);

When I run node index.js The log console shows:

before
1
2
3
Promise { <pending> }
after

My function getAWSKeys is async, and I'm using await for the call to SSM.getParameters. I really expect:

before
after
1
2
3
'my params'

Why execution doesn't wait for SSM.getParameters?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Perimosh
  • 2,304
  • 3
  • 20
  • 38
  • 1
    You're not `await`ing `getAWSKeys`. – tkausl Aug 16 '20 at 22:57
  • 3
    `await` doesn't pause execution, the code is still asynchronous. It might help you understand it better if you replaced `await` with `.then()` callbacks – Phil Aug 16 '20 at 22:57
  • thanks to all of you. I'm new to promises and js. How to use 'then()' exactly? – Perimosh Aug 16 '20 at 23:01
  • 3
    Using then() is a fundamental part of using promises and will be covered in every basic promises tutorial as well as in documentation. If I were you I would forget that await exists for a while until you really can conquer using promises – charlietfl Aug 16 '20 at 23:03
  • I will reply my own answer. I have found a way to accomplish what I need. Anyaway, please post your answers guys, they really helped me, I will vote up. – Perimosh Aug 16 '20 at 23:03

0 Answers0