Consider this function which mocks querying a database, and returning some records after 2 seconds, via a Promise:
function query() {
var recs = [1,2,3];
return new Promise(r => setTimeout(() => r(recs), 2000))
}
If I add the following statement:
query().then(console.log);
and run the script the with node, it pauses 2 seconds, prints out the records, and returns, as expected.
On the other hand, if I replace that line with an array of the exact same promises:
Array(5).map(x => query().then(console.log));
The script returns immediately, and prints nothing. Why does node wait for the single promise to return before exiting, but exit immediately when there's an array of 5 unresolved promises?