Marc-André Bouchard
12/16/2024, 2:39 PMfunction myFunction(listOfThings) {
return new Promise((resolve, reject) => {
let promises = []
for (let thing of Object.values(listOfThings)) {
log.debug("What time is it?", new Date());
promises.push(queryThatSystem(thing));
}
Promise.allSettled(promises).then((myPromise) => {
// [Do stuff]
}
});
}
function queryThatSystem(thing) {
return new Promise((resolve, reject) => {
// [Do more stuff]
});
}
Notice the log that I put in there. The output of that log has anywhere between 0.5 and 3 seconds of difference between each iteration of the loop, telling me that instead of just filling the array of promises and then letting them all execute at once, it seems that it's putting the promises in a queue, waiting for one of them to be done before getting to work on the other.
My bet is that I structured my promises wrong and I'm missing something fundamental here, but I could use a second glance since I'm not too sure where I've erred in making that bit of code. 🤔Marc-André Bouchard
12/16/2024, 4:32 PMqueryThatSystem()
notably ran a https.request
.
...That is, not a https.request.promise
. That one boo-boo caused the function to run synchronously instead of asynchronously.
Switching the HTTPS request to a promise restored asynchronicity, and now my log traces clearly indicate that every iteration of the loop is running at the same time.erictgrubaugh
12/16/2024, 5:39 PM