Question: I'm learning Python. I have a doubt.

```
pet_0 = {
    'owner_name' : 'john',
    'pet_animal' : 'dog',
    'pet_name' : 'ghost',
    'age' : 8,
    'diet' : 'mutton',
    }

pet_1 = {
    'owner_name' : 'arya',
    'pet_animal' : 'cat',
    'pet_name' : 'nymeria',
    'age' : 6,
    'diet' : 'chicken',
    }

pet_2= {
    'owner_name' : 'bran',
    'pet_animal' : 'crow',
    'pet_name' : 'summer',
    'age' : 2,
    'diet' : 'worms',
    }

pets = [pet_0, pet_1, pet_2]

for pet in pets:

    print(f"\nDetails about {pet['pet_name'].title()}:")

    for key, value in pet.items():
        print(f"\t{key}: {value}")

```

The above print function works perfectly, but when I use:

```
for key, value in pet.items():
    print(f"\t{key.title()}: {value.title()}")

```

There is an error because a value in the dictionary is a integer.
So how to use title method in print function, if one of the value in the dictionary is an int?

Answer: 
The problem is that `useState` here gives you the `dati` variable with a specific value. Then your asynchronous stuff happens, and `setDati()` is called multiple times, but `dati` is does not change until the form is re-rendered and you then call `onFormSubmit` again.

You have a few options.

You could add the results only once as an array.

```
const results = []

for(let i=0; i < daysDeparture.length; i++){
    dayDeparture = daysDeparture[i];
    const res = await axios.get('https://api.skypicker.com/flights?flyFrom='+placeFrom+'&to='+placeTo+'&dateFrom='+dayDeparture+'&dateTo='+dayDeparture+'&partner=picky');
    results.push(res.data);
}

// Add all results when all are fetched.
setDati([...dati, ...results])

```

Or [`useReducer`](https://reactjs.org/docs/hooks-reference.html#usereducer) which reliably gives you the latest version of the state, right at the time that you change it, so you don't have stale data.

```
// Something like...

function reducer(state, action) {
  switch(action.type) {
    case 'append':
      return [...state, action.payload]
  }
}

function YourComponent() {
  const [dati, dispatch] = useReducer(reducer, [])

  const onFormSubmit = async (e) => {
    for(let i=0; i < daysDeparture.length; i++){
        dayDeparture = daysDeparture[i];
        const res = await axios.get('https://api.skypicker.com/flights?flyFrom='+placeFrom+'&to='+placeTo+'&dateFrom='+dayDeparture+'&dateTo='+dayDeparture+'&partner=picky')
        dispatch({type: 'append', payload: res.data})
    }
  }
}

```
useState provides a way to use the previous state when updating (just like the callback way when you are using a class component), could give that a shot

```
        .then(res => {
            setDati(prevDati => ([...prevDati, res.data]));
            onChildSubmit(dati);

        })

```