In node.js I am trying to loop through some items, complete an async process for each one and then wait for each to be complete before the next one starts. I must be doing something wrong as the Promise.all() is not waiting for any of the async processes to complete! My code is below:
getChildLessons() {
return new Promise((resolve, reject) => {
Promise.all(
//nested for loop is needed to get all information in object / arrays
this.lessons.levels.map((item, i) => {
item.childlevels.map((childItem, iChild) => {
return ((i, iChild) => {
//return async process with Promise.resolve();
return this.horseman
.open(childItem.url)
.html()
.then((html) => {
//adding some information to an array
})
.then(() => {
return Promise.resolve();
}).catch((err) => {
reject(err);
});
})(i, iChild);
});
})
// Promise.all().then()
).then(() => {
resolve(this.lesson);
})
.catch((err) => {
console.log(err);
});
});
}
I am fairly new to async with node.js so please could you provide a clear example if possible.