does anyone have any guides or tips on how to write a nested promises?
I am trying to turn the following nested
for loop
let arr = [1, 2, 3, 4, 5]
for (i = 0; i < arr.length ; i++) {
for (j = i + 1; j < arr.length ; j++) {
// call another asynchronous function
}
}
Promise.all
for
j = i + 1
Promise.all
Push them into an array
let arr = [1, 2, 3, 4, 5]
const promises = []
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
promises.push(new Promise((resolve, reject) => {
setTimeout(resolve, 100, [i,j]);
}));
}
}
Promise.all(promises).then(values => {
console.log(values);
});