'API request in a loop works for two iterations and then crashes out

This query loops through a data object getNewNodes and for each install_key it makes an API call to get a hash of the key by triggering getBcrypt. Sometimes it does the first one or two but then it falls over itself and produces an error "state1.value[i].hash" is not defined. State 1 is a temporary state variable and 'hash' is one of the keys inside the API response. I assumed that the onsuccess: function(data) would return the data from the API response once there was a response but it seems that's not the case. Could one of you kind folks please tell me where I'm going wrong.

const array_key = getNewNodes.data.install_key;
let newData = [];
let i=0
async function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}
async function makeRequests(secure_key){
for (const install_key of array_key){
 
 await getBcrypt.trigger({
  additionalScope: {install_key:install_key},
  onSuccess: function(data){
  state1.setIn([i],data)
  }})
        newData.push({
        install_key: install_key,
        secure_key: state1.value[i].hash
        })  
  console.log(state1.value[i].hash)

i++ }
return newData();
}
return makeRequests();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source