Is there a way to setState of an array in a .then() block?

`

  1. I have an array of points (city, state)
  2. I make a fetch call to convert my points to (lat and long) so I push the result to an array and tried to setState of the array but when I console.log(array) I got []. `
if (pointsComplete) {
        // for  (let i = 0; i < pointsComplete.length; i++) {
        for await (let x of pointsComplete) {
          if (x.city !== "") {
            fetch(
              `https://singlesearch.alk.com/na/api/search?&query=${x.city} , ${x.state}&maxResults=1`,
              {
                method: "GET",
                headers: {
                  "Content-Type": "application/json",
                  Authorization: trimble_key,
                },
              }
            )
              .then((response) => response.json())
              .then((data) => {
                pointsCoords.push({
                  Lat: data.Locations[0].Coords.Lat,
                  Long: data.Locations[0].Coords.Lon,
                  position: parseInt(x.position),
                });
              })
              .finally(() => {
                setPointsCoordsArray(pointsCoords);
              });
          }
        }
      }

Answer

This worked for me ..

if (pointsComplete) {
        let promises = [];
        for (let x of pointsComplete) {
          if (x.city !== "") {
            promises.push(
              fetch(
                `https://singlesearch.alk.com/na/api/search?&query=${x.city} , ${x.state}&maxResults=1`,
                {
                  method: "GET",
                  headers: {
                    "Content-Type": "application/json",
                    Authorization: trimble_key,
                  },
                }
              )
                .then((response) => response.json())
                .then((data) => {
                  pointsCoords.push({
                    Lat: data.Locations[0].Coords.Lat,
                    Long: data.Locations[0].Coords.Lon,
                    position: parseInt(x.position),
                  });
                })
            );
          }
        }

        Promise.all(promises).then((data) => {
          setPointsCoordsArray(pointsCoords);
          console.log("______________");
       
        });
      }