Q:

For Loop with Multiple Async calls - Repeatedly Prints Last Item in Second Async Function

I am looping through a bunch of posts and doing multiple async calls within the loop.  I believe I understand the issue, but am hoping for an alternative solution than the one that comes to mind for me. By the time the first async call finishes and triggers the second async call, all of the postIDs have been looped through and the postID is now set to the last postID. 
var postIDs = {
    "abcdef": true
    "bbb456": true
    "ccc123": true
}

for(var postID in postIDs) {
  console.log("postID = " + postID);
  // check that the postID is within the postIDs to skip inherited properties
  if (postIDs.hasOwnProperty(postID)) {
    // make one async call
    admin.database().ref().child('posts').child(postID).limitToLast(1).once('value').then(snapshotForMostRecentPost => {    
      // make a second async call
      admin.database().ref().child('anotherBranch').child('someChild').once('value').then(snapshotForSomeOtherStuff => {
        console.log("postID = " + postID) // **ISSUE**: the postID is always `ccc123`
        // do some more stuff with the postID
      })
    })
  }
}

The result I am aiming for is this:
abcdef
bbb456
ccc123 

Instead, I get this result:
ccc123
ccc123
ccc123 

Possible Solution
One way I can think of to resolve this is by putting the async calls into their own function and calling that function, like so:
var postIDs = {
    "abcdef": true
    "bbb456": true
    "ccc123": true
}

for(var postID in postIDs) {
  console.log("postID = " + postID);
  // check that the postID is within the postIDs to skip inherited properties
  if (postIDs.hasOwnProperty(postID)) {
    triggerThoseAsyncCalls(postID)
  }
}

function triggerThoseAsyncCalls(postID) {
  // make one async call
  admin.database().ref().child('posts').child(postID).limitToLast(1).once('value').then(snapshotForMostRecentPost => {    
    // make a second async call      
    admin.database().ref().child('anotherBranch').child('someChild').once('value').then(snapshotForSomeOtherStuff => {
      console.log("postID = " + postID)
    })
  })
}

I would, however, prefer to keep this as one function.  Does anyone know of a way to resolve this without separating out the async calls into a separate function?

A:

Use let instead:     
for(let postID in postIDs) { ... }

let has the feature of rebinding the loop variable on each iteration. 
Other than let you can use postIDs.foreach()

