-1

I'm trying to get a list of organization account id's from our AWS account.

I have the following code

const acc_list: string[] = [];

(async () => {
  const orgs = await aws.organizations.getOrganization();
  orgs.accounts.forEach(account => 
    acc_list.push(account.id))
})()

console.log(acc_list)

Which logs an empty list because clearly the console command is running before the promise.

My goal is I want to send the list of accounts to a different function (different file) in my typescript application. No clue how to do that.

mbspark
  • 304
  • 1
  • 10
  • Does this answer your question? [How to return many Promises in a loop and wait for them all to do other stuff](https://stackoverflow.com/questions/31426740/how-to-return-many-promises-in-a-loop-and-wait-for-them-all-to-do-other-stuff) – CertainPerformance Jan 23 '20 at 01:43

2 Answers2

0

I'd suggest you read through https://javascript.info/async-await

  • async takes a function (....) -> T to (....) -> Promise
  • await takes a Promise to T, but only inside an async function

If you're having problems with async/await, start by using the promise api directly.

const accountIDs = (org) => orgs.accounts.map(account => (account.id))

const fetchAccountIDs = async () => accountIDs(await aws.organizations.getOrganization())

const promisedAccountIds = fetchAccountIDs()

promisedAccountIds.then(ids => console.log(ids))

The big rule about programming with promises is that the data they contain can never leave the promise. So trying to capture it in a list like that is a big no no. The worst thing that can happen doing that is actually when it works. Because there's no way to tell what might cause it to stop working, and if that happens a year from now good luck figuring out why it broke or why it worked in the first place.

Keynan
  • 1,318
  • 1
  • 10
  • 17
  • So really I need to go call the function each time I want the list and iterate it through at that time? – mbspark Jan 23 '20 at 02:04
  • repeated invokations of `promisedAccountIds.then(ids => ...)` will not result in additional network traffic. Only `fetchAccountIDs()` will trigger a network event / delay. – Keynan Jan 24 '20 at 05:44
0

The problem is that the function you've created async () => { ... } actually returns a Promise that you still need to await. So, wrapping an asynchronous code into such async lambda doesn't make sence because the code block remains asynchronous. I can suggest you this tutorial.

A solution depends on the problem context, might be the whole block should be asynchronous, like:

async function printAccountIds() {
  const orgs = await aws.organizations.getOrganization();
  const accountIds = orgs.accounts.map(account => account.id);
  console.log(accountIds);
}

Or you can just subscribe to the promise, like:

aws.organizations.getOrganization().then(orgs => {
  const accountIds = orgs.accounts.map(account => account.id);
  console.log(accountIds);
});
Valeriy Katkov
  • 11,518
  • 2
  • 37
  • 69