I am new to asynchronous operations in JS using promises or async/await and I am trying to wrap my head around the concept. One issue I've come across is how to maintain a state while looping over multiple arrays and executing an asynchronous operation on each of the elements. For example, I have the following code:
const listOfStores = [{location: '123 testers lane'}, {location: '321 testers lane'}];
const listofTechs = [{location: '123 nowhere lane'}];
for(let i = 0; i < listOfStores.length; i++){
let store = listOfStores[i];
let bestDistance = Infinity;
for(let j = 0; j < listOfTechs.length; j++){
let tech = listOfTechs[j];
let distance = await getDistance(store.location, tech.location);
bestDistance = Math.min(bestDistance, distance);
}
console.log('Closest Tech Is ' + bestDistance + ' Miles Away')
}
async function getDistance(store, tech) {
return new Promise(function(resolve, reject){
// THIS WOULD ACTUALLY BE AN AJAX CALL TO AN API
window.setTimeout(function(){
resolve( Math.floor(Math.random() * (100 - 25) + 25));
}, 2000);
});
}
While the above code suits my purpose I am trying to get a better understanding of Promise-based code in general. I realize that the loops must be synchronous in order for the logic to work, but API calls are inherently asynchronous. I am not trying to create asynchronous code, but rather I am trying to deal with asynchronous code. Pausing the code execution and waiting for the promise to resolve with await works well. And it is my understanding that Promise-based code can be rewritten as async/await code and vice versa so my question is how can I rewrite the code above without using async/await? As far as I can tell there is no way to pause execution or maintain a state with regular promises.
This problem seems common enough that someone would have developed a pattern for dealing with it already but so far I have not been able to find that pattern.
Note: Each loop must finish before moving onto the next. I considered looping through the stores and techs arrays and creating a promise that would eventually resolve to the distance between that tech and store, and then store each of these promises in an array, and using Promise. all()
to resolve all the individual promises. But this will not work. First, because there could potentially be thousands of stores, and resolving each one at the same time will cause issues with rate limiting. Second Promise. all()
will fail if one of the promises is rejected since there could be thousands of promises one is bound to fail and cause all the rest to fail as well.
Any advice is greatly appreciated
Thanks