I am developing an application in React. It has a simple search box, on submit , i am calling api1, which returns an array of data. For each of the item in the array, i need to call api2 - which should be async. Currently the code is something like this:
onSubmit() {
api1(textboxvalue).then(data => {
let currentComponent = this
if(data matches condition1) {
currentComponent.state.arrayOfElements1.push(data);
}
if(data matches condition2) {
currentComponent.state.arrayOfElements2.push(data);
}
const arrayOfElements1 = this.state.arrayOfElements1
arrayOfElements1.map(function (element) {
api2(element) -> set this.state.data1loaded
})
const arrayOfElements2 = this.state.arrayOfElements2
arrayOfElements2.map(function (element) {
api2(element) -> set this.state.data2loaded
})
}
Requirement is check data1loaded and data2loaded states in render asynchronously. But with the above code, render is called only after promise of api1 completes
Any thoughts ?