As the questions says I'm trying to wait for a map to finish executing before I run another command. In this case console log. I have
let concernsInDB = [];
let ctr = 0;
this.state.templateOptions.map(el => {
el.details.map(elDetail => {
this.getConcernsDB(elDetail.values)
.then(elDetailResults => {
let detailsToPush = {};
if(elDetailResults.length) {
detailsToPush[elDetailResults[0].value_id] = elDetailResults;
concernsInDB.push(detailsToPush);
}
})
})
})
console.log(concernsInDB);
The above code executes and I get a console.log of Array []. The console.log is resolving before all other methods are finished.
this.state.templateOptions contains an array of several objects (about 50, so I'm not putting all here):
Object {
"active": 1,
"id": 1378,
"name": "Wires are running through trees",
"narrative": "Service drop wires are running through tree(s).It is recommended to have a contacting utility company or a qualified electrician and/or tree s",vice company to correct conditions as necessary.
"value_id": 13935,
},
As I loop through them I run this method: this.getConcernsDB(elDetail.values)
elDetail.values is the "value_id" from the above example. I then wait for it to resolve .then push the results into concernsInDB. This has to be done on several records. How would I wait for it all to finish before console.logging concernsInDB so it'll have all the completed data?
If I wrap it in a promise the resolve would run on each loop so I don't see how I could accomplish it. Any solutions?