However array2 is blank. I have seen that JavaScript doesn't stop like C# does when calling a function. How would I rewrite this so this works as I expect?

node.js

loops

There are a couple answers to this, and probably the "correct" way would be to not fetch each username individually like that but to write the query to return all the usernames at once. If you want to keep individual username queries, you can do something like:

What I added was a conditional on the callback to console.log the results only if the number of returned usernames equals the number of userids in the original array. That indicates that all of the queries have completed.

You will also have to handle the case where there is an error in GetUserName, because that could cause console.log() to never be called assuming you're planning on handling the error and not just letting it crash your application.

Disclaimer; I'm not very used to JavaScript (or Node) myself. Anyway, my take is something like this might work: