doubt with the result of a fix

0

I have a question about the result of an arrangement that I believe in an iteration and I do not know why the code is the following: This piece of code is an object which I will use to dynamically manage a direction that I will bring with the function fecth ()

const dataSet = {
    "EMISS.CO2-TOTV-TT-TO-MD.A" : ["Maryland", {lat: 39.063946, lng: -76.802101}],//
	"EMISS.CO2-TOTV-TT-TO-AL.A" : ["Alabama", {lat: 32.806671, lng: -86.791130}],//
	"EMISS.CO2-TOTV-TT-TO-AK.A" : ["Alaska", {lat: 61.370716, lng: -152.404419}],//
	"EMISS.CO2-TOTV-TT-TO-AZ.A" : ["Arizona", {lat: 33.729759, lng: -111.431221}],//
	"EMISS.CO2-TOTV-TT-TO-AR.A" : ["Arkansas", {lat: 34.969704, lng: 92.373123}],//
	"EMISS.CO2-TOTV-TT-TO-CA.A" : ["California", {lat: 36.116203, lng: -119.681564}],
	"EMISS.CO2-TOTV-TT-TO-CO.A" : ["Colorado", {lat: 39.059811, lng: -105.311104}],//
	"EMISS.CO2-TOTV-TT-TO-CT.A" : ["Connecticut",{lat: 41.597782, lng: -72.755371} ],//
	"EMISS.CO2-TOTV-TT-TO-DE.A" : ["Delaware", {lat: 39.318523, lng: -75.507141}] ,//
	"EMISS.CO2-TOTV-TT-TO-DC.A" : ["District of Columbia", {lat: 38.897438, lng: -77.026817}],//
	"EMISS.CO2-TOTV-TT-TO-FL.A" : ["Florida", {lat: 27.766279, lng: -81.686783}],//
	"EMISS.CO2-TOTV-TT-TO-GA.A" : ["Georgia", {lat: 33.040619, lng: -83.643074}],//
	"EMISS.CO2-TOTV-TT-TO-HI.A" : ["Hawaii", {lat: 21.094318, lng: -157.498337}],//
	"EMISS.CO2-TOTV-TT-TO-ID.A" : ["Idaho", {lat: 44.240459, lng: -114.478828}],//
	"EMISS.CO2-TOTV-TT-TO-IL.A" : ["Illinois", {lat: 40.349457, lng: -88.986137}],//
	"EMISS.CO2-TOTV-TT-TO-IN.A" : ["Indiana", {lat: 39.849426, lng: -86.258278}],//
	"EMISS.CO2-TOTV-TT-TO-IA.A" : ["Iowa", {lat: 42.011539, lng: -93.210526}],//
	"EMISS.CO2-TOTV-TT-TO-KS.A" : ["Kansas", {lat: 38.526600, lng: -96.726486}],//
	"EMISS.CO2-TOTV-TT-TO-KY.A" : ["Kentucky", {lat: 37.668140, lng: -84.670067}],//
	"EMISS.CO2-TOTV-TT-TO-LA.A" : ["Louisiana", {lat: 31.169546, lng: -91.867805}],//
	"EMISS.CO2-TOTV-TT-TO-ME.A" : ["Maine", {lat: 44.693947, lng: -69.381927}],//
	"EMISS.CO2-TOTV-TT-TO-MA.A" : ["Massachusetts", {lat: 42.230171, lng: -71.530106}],//
	"EMISS.CO2-TOTV-TT-TO-MI.A" : ["Michigan", {lat: 43.326618, lng: -84.536095}],//
	"EMISS.CO2-TOTV-TT-TO-MN.A" : ["Minnesota", {lat: 45.694454, lng: -93.900192}],//
	"EMISS.CO2-TOTV-TT-TO-MS.A" : ["Mississippi", {lat: 32.741646, lng: -89.678696}],//
	"EMISS.CO2-TOTV-TT-TO-MO.A" : ["Missouri", {lat: 38.456085, lng: -92.288368}],//
	"EMISS.CO2-TOTV-TT-TO-MT.A" : ["Montana", {lat: 46.921925, lng: -110.454353}],//
	"EMISS.CO2-TOTV-TT-TO-NE.A" : ["Nebraska", {lat: 41.125370, lng: -98.268082}],//
	"EMISS.CO2-TOTV-TT-TO-NV.A" : ["Nevada", {lat: 38.313515, lng: -117.055374}],//
	"EMISS.CO2-TOTV-TT-TO-NH.A" : ["New Hampshire", {lat: 43.452492, lng: -71.563896}],//
	"EMISS.CO2-TOTV-TT-TO-NJ.A" : ["New Jersey", {lat: 40.298904, lng: -74.521011}],//
	"EMISS.CO2-TOTV-TT-TO-NM.A" : ["New Mexico", {lat: 34.840515, lng: -106.248482}],//
	"EMISS.CO2-TOTV-TT-TO-NY.A" : ["New York", {lat: 42.165726, lng: -74.948051}],//
	"EMISS.CO2-TOTV-TT-TO-NC.A" : ["North Carolina", {lat: 35.630066, lng: -79.806419}],//
	"EMISS.CO2-TOTV-TT-TO-ND.A" : ["North Dakota", {lat: 47.528912, lng: -99.784012}],//
	"EMISS.CO2-TOTV-TT-TO-OH.A" : ["Ohio", {lat: 40.388783, lng: -82.764915}],//
	"EMISS.CO2-TOTV-TT-TO-OK.A" : ["Oklahoma", {lat: 35.565342, lng: -96.928917}],//
	"EMISS.CO2-TOTV-TT-TO-OR.A" : ["Oregon", {lat: 44.572021, lng: -122.070938}],//
	"EMISS.CO2-TOTV-TT-TO-PA.A" : ["Pennsylvania", {lat: 40.590752, lng: -77.209755}],//
	"EMISS.CO2-TOTV-TT-TO-RI.A" : ["Rhode Island", {lat: 41.680893, lng: -71.511780}],//
	"EMISS.CO2-TOTV-TT-TO-SC.A" : ["South Carolina", {lat: 33.856892, lng: -80.945007}],//
	"EMISS.CO2-TOTV-TT-TO-SD.A" : ["South Dakota", {lat: 44.299782, lng: -99.438828}],//
	"EMISS.CO2-TOTV-TT-TO-TN.A" : ["Tennessee", {lat: 35.747845, lng: -86.692345}],//
	"EMISS.CO2-TOTV-TT-TO-TX.A" : ["Texas", {lat: 31.054487, lng: -97.563461}],//
	"EMISS.CO2-TOTV-TT-TO-UT.A" : ["Utah", {lat: 40.150032, lng: -111.862434}],//
	"EMISS.CO2-TOTV-TT-TO-VT.A" : ["Vermont", {lat: 44.045876, lng: -72.710686}],//
	"EMISS.CO2-TOTV-TT-TO-VA.A" : ["Virginia", {lat: 37.769337, lng: -78.169968}],//
	"EMISS.CO2-TOTV-TT-TO-WA.A" : ["Washington", {lat: 47.400902, lng: -121.490494}],//
	"EMISS.CO2-TOTV-TT-TO-WV.A" : ["West Virginia", {lat: 38.491226, lng: -80.954453}],//
	"EMISS.CO2-TOTV-TT-TO-WI.A" : ["Wisconsin", {lat: 44.268543, lng: -89.616508}],//
	"EMISS.CO2-TOTV-TT-TO-WY.A" : ["Wyoming", {lat: 42.755966, lng: -107.302490}]//
}

here comes the next piece of code I comment there to explain in a more comfortable way

const keyDataSet = Object.keys(dataSet)//me devuelve un arreglo con el nombre de las propiedades
var locationsInfo = []//en este arreglo voy a guardar los objetos que voy a iterar mas abajo

const getLocations = () => {

for(let id of keyDataSet){
    
    
    fetch('http://api.eia.gov/series/?api_key=707c8ecbdbd34cbb75272c7a4256ca6b&series_id=${id}')
    .then(response => response.json())
    .then(locations =>{

       let locationData = {//creo un objeto donde guardo la informacion
            position:{//creo otro objeto
                 lat:dataSet[id][1].lat,lng:dataSet[id][1].lng},
            name:dataSet[id][0],
            year:locations.series[0].data[0][0], 
            CO:  locations.series[0].data[0][1]            
        }
        
        locationsInfo.push(locationData)  
  
    })   
}

}

the problem is here I call the function of getLocations () then I print the value of the arrangement that contains all the objects and prints them normally, but then I print the length of that arrangement and it appears zero and I do not know the truth of why it happens that I would be very grateful if you told me why I do not know if it's because of the .then

getLocations()
console.log(locationsInfo);//me arroja el arreglo con todos los objetos añadidos y con una medida de 51

console.log(locationsInfo.length);// pero aca me dice que ese arreglo tiene una medida de cero y quedo como WTF!

On the other hand when I print in console this appears:

What difference there is between the first arrangement does not tell me how many indexes there are but in the other it tells me that there are 30 indexes, but if I open the first one that tells me that there are zero indices, this appears:

    
asked by okmi santos 22.11.2018 в 23:36
source

1 answer

0

What happens is that these combined synchronous calls with the asynchronous ones that the fetch has, therefore, when you make the call to getLocations() and still do not finish making all the requests. What you should do is return a promise in the function getLocation() to get the result once all the requests are finished. I leave the example:

const dataSet = {
    "EMISS.CO2-TOTV-TT-TO-MD.A" : ["Maryland", {lat: 39.063946, lng: -76.802101}],//
	"EMISS.CO2-TOTV-TT-TO-AL.A" : ["Alabama", {lat: 32.806671, lng: -86.791130}],//
	"EMISS.CO2-TOTV-TT-TO-AK.A" : ["Alaska", {lat: 61.370716, lng: -152.404419}],//
	"EMISS.CO2-TOTV-TT-TO-AZ.A" : ["Arizona", {lat: 33.729759, lng: -111.431221}],//
	"EMISS.CO2-TOTV-TT-TO-AR.A" : ["Arkansas", {lat: 34.969704, lng: 92.373123}],//
	"EMISS.CO2-TOTV-TT-TO-CA.A" : ["California", {lat: 36.116203, lng: -119.681564}],
	"EMISS.CO2-TOTV-TT-TO-CO.A" : ["Colorado", {lat: 39.059811, lng: -105.311104}],//
	"EMISS.CO2-TOTV-TT-TO-CT.A" : ["Connecticut",{lat: 41.597782, lng: -72.755371} ],//
	"EMISS.CO2-TOTV-TT-TO-DE.A" : ["Delaware", {lat: 39.318523, lng: -75.507141}] ,//
	"EMISS.CO2-TOTV-TT-TO-DC.A" : ["District of Columbia", {lat: 38.897438, lng: -77.026817}],//
	"EMISS.CO2-TOTV-TT-TO-FL.A" : ["Florida", {lat: 27.766279, lng: -81.686783}],//
	"EMISS.CO2-TOTV-TT-TO-GA.A" : ["Georgia", {lat: 33.040619, lng: -83.643074}],//
	"EMISS.CO2-TOTV-TT-TO-HI.A" : ["Hawaii", {lat: 21.094318, lng: -157.498337}],//
	"EMISS.CO2-TOTV-TT-TO-ID.A" : ["Idaho", {lat: 44.240459, lng: -114.478828}],//
	"EMISS.CO2-TOTV-TT-TO-IL.A" : ["Illinois", {lat: 40.349457, lng: -88.986137}],//
	"EMISS.CO2-TOTV-TT-TO-IN.A" : ["Indiana", {lat: 39.849426, lng: -86.258278}],//
	"EMISS.CO2-TOTV-TT-TO-IA.A" : ["Iowa", {lat: 42.011539, lng: -93.210526}],//
	"EMISS.CO2-TOTV-TT-TO-KS.A" : ["Kansas", {lat: 38.526600, lng: -96.726486}],//
	"EMISS.CO2-TOTV-TT-TO-KY.A" : ["Kentucky", {lat: 37.668140, lng: -84.670067}],//
	"EMISS.CO2-TOTV-TT-TO-LA.A" : ["Louisiana", {lat: 31.169546, lng: -91.867805}],//
	"EMISS.CO2-TOTV-TT-TO-ME.A" : ["Maine", {lat: 44.693947, lng: -69.381927}],//
	"EMISS.CO2-TOTV-TT-TO-MA.A" : ["Massachusetts", {lat: 42.230171, lng: -71.530106}],//
	"EMISS.CO2-TOTV-TT-TO-MI.A" : ["Michigan", {lat: 43.326618, lng: -84.536095}],//
	"EMISS.CO2-TOTV-TT-TO-MN.A" : ["Minnesota", {lat: 45.694454, lng: -93.900192}],//
	"EMISS.CO2-TOTV-TT-TO-MS.A" : ["Mississippi", {lat: 32.741646, lng: -89.678696}],//
	"EMISS.CO2-TOTV-TT-TO-MO.A" : ["Missouri", {lat: 38.456085, lng: -92.288368}],//
	"EMISS.CO2-TOTV-TT-TO-MT.A" : ["Montana", {lat: 46.921925, lng: -110.454353}],//
	"EMISS.CO2-TOTV-TT-TO-NE.A" : ["Nebraska", {lat: 41.125370, lng: -98.268082}],//
	"EMISS.CO2-TOTV-TT-TO-NV.A" : ["Nevada", {lat: 38.313515, lng: -117.055374}],//
	"EMISS.CO2-TOTV-TT-TO-NH.A" : ["New Hampshire", {lat: 43.452492, lng: -71.563896}],//
	"EMISS.CO2-TOTV-TT-TO-NJ.A" : ["New Jersey", {lat: 40.298904, lng: -74.521011}],//
	"EMISS.CO2-TOTV-TT-TO-NM.A" : ["New Mexico", {lat: 34.840515, lng: -106.248482}],//
	"EMISS.CO2-TOTV-TT-TO-NY.A" : ["New York", {lat: 42.165726, lng: -74.948051}],//
	"EMISS.CO2-TOTV-TT-TO-NC.A" : ["North Carolina", {lat: 35.630066, lng: -79.806419}],//
	"EMISS.CO2-TOTV-TT-TO-ND.A" : ["North Dakota", {lat: 47.528912, lng: -99.784012}],//
	"EMISS.CO2-TOTV-TT-TO-OH.A" : ["Ohio", {lat: 40.388783, lng: -82.764915}],//
	"EMISS.CO2-TOTV-TT-TO-OK.A" : ["Oklahoma", {lat: 35.565342, lng: -96.928917}],//
	"EMISS.CO2-TOTV-TT-TO-OR.A" : ["Oregon", {lat: 44.572021, lng: -122.070938}],//
	"EMISS.CO2-TOTV-TT-TO-PA.A" : ["Pennsylvania", {lat: 40.590752, lng: -77.209755}],//
	"EMISS.CO2-TOTV-TT-TO-RI.A" : ["Rhode Island", {lat: 41.680893, lng: -71.511780}],//
	"EMISS.CO2-TOTV-TT-TO-SC.A" : ["South Carolina", {lat: 33.856892, lng: -80.945007}],//
	"EMISS.CO2-TOTV-TT-TO-SD.A" : ["South Dakota", {lat: 44.299782, lng: -99.438828}],//
	"EMISS.CO2-TOTV-TT-TO-TN.A" : ["Tennessee", {lat: 35.747845, lng: -86.692345}],//
	"EMISS.CO2-TOTV-TT-TO-TX.A" : ["Texas", {lat: 31.054487, lng: -97.563461}],//
	"EMISS.CO2-TOTV-TT-TO-UT.A" : ["Utah", {lat: 40.150032, lng: -111.862434}],//
	"EMISS.CO2-TOTV-TT-TO-VT.A" : ["Vermont", {lat: 44.045876, lng: -72.710686}],//
	"EMISS.CO2-TOTV-TT-TO-VA.A" : ["Virginia", {lat: 37.769337, lng: -78.169968}],//
	"EMISS.CO2-TOTV-TT-TO-WA.A" : ["Washington", {lat: 47.400902, lng: -121.490494}],//
	"EMISS.CO2-TOTV-TT-TO-WV.A" : ["West Virginia", {lat: 38.491226, lng: -80.954453}],//
	"EMISS.CO2-TOTV-TT-TO-WI.A" : ["Wisconsin", {lat: 44.268543, lng: -89.616508}],//
	"EMISS.CO2-TOTV-TT-TO-WY.A" : ["Wyoming", {lat: 42.755966, lng: -107.302490}]//
}
const keyDataSet = Object.keys(dataSet);

const getLocations = () => {
    let data = [];
    let locationsInfo = [];
    return new Promise((resolve, reject) => {
        for(let id of keyDataSet){
            data.push(
                fetch('https://api.eia.gov/series/?api_key=707c8ecbdbd34cbb75272c7a4256ca6b&series_id=${id}')
            );
            locationsInfo.push({
                position:{
                    lat: dataSet[id][1].lat,
                    lng:dataSet[id][1].lng
                },
                name:dataSet[id][0]         
            })
        }
        Promise.all(data)
        .then(responses => 
            Promise.all(responses.map(response => response.json()))
        )
        .then(locations => {
            locations.forEach((location, index) => {
                locationsInfo[index].year = location.series[0].data[0][0]
                locationsInfo[index].CO = location.series[0].data[0][1]
            })
            resolve(locationsInfo)
        })
    })
}
getLocations()
.then(locations => {
    console.log(locations)
    console.log(locations.length)
})

There you can make the consultation of your locations within .then .

The problem that the console.log shows you the results seems to be a problem of Google chrome synchronization or webkit, I leave you a link where they talk about the topic.

    
answered by 23.11.2018 / 19:37
source