Franco Agüero
Franco Agüero

Reputation: 23

how to execute a function after end another in react native?

I have a query that I have a function which response to a server, so it takes a while and when it tries to execute the other function, an error occurs. Since the function responds an array from which comes from the server and when the other function requests that, even It does not have. Have to solve this. example :

responseServer(tarea) {
     return fetch(url)
      .then((response) => response.json())
      .then((responseJson) => {
        this.setState({ refresh: false })
        global.refresh = false
        let data = [];
        let Milestone = [];

        Object.values(responseJson.summary).forEach(item => {
          data = data.concat(item);
        });

        const result = ["Milestone"]
          .reduce((total, category) => ({
            ...total,
            [category]: data.filter(line => line.includes(category)) 
          }), {})

        Object.values(result.Milestone).forEach(item => {
          Milestone = Milestone.concat(item)
        });

        this.setState({
          Milestone: Milestone
        })
      })
}

and another function

sacarPorcentaje(item) {
 this.responseServer(item)
 let summaryCopy = this.state.Milestone.map(data => {return data.split(",")})
      console.log(summaryCopy)
      var Okeys = 0;
      var total = 0;
      for (var i = 0; i < summaryCopy.length; i++){
        for(var j = 0; j < summaryCopy[i].length; j++){
          if(summaryCopy[i][j] === "OK") {
            Okeys = Okeys + 1
          }
        }
        total = total + 1 
      }
      console.log(Okeys)
      console.log(total)
}

Upvotes: 0

Views: 1648

Answers (2)

Jebin Benny
Jebin Benny

Reputation: 951

You should update sacarPorcentaje() to async function. then await the function call, will resolve your issue

async sacarPorcentaje (item) {
 await this.responseServer(item)
 let summaryCopy = this.state.Milestone.map(data => {return data.split(",")})
      console.log(summaryCopy)
      var Okeys = 0;
      var total = 0;
      for (var i = 0; i < summaryCopy.length; i++){
        for(var j = 0; j < summaryCopy[i].length; j++){
          if(summaryCopy[i][j] === "OK") {
            Okeys = Okeys + 1
          }
        }
        total = total + 1 
      }
      console.log(Okeys)
      console.log(total)
}

There is also one more solution for this issue. please search how to use Promise in JS

Upvotes: 1

josecastillo86
josecastillo86

Reputation: 330

I think you need to use then when you call responseServer function in sacarPorcentaje function.

sacarPorcentaje(item) {
 this.responseServer(item).then(() => {
 let summaryCopy = this.state.Milestone.map(data => {return data.split(",")})
      console.log(summaryCopy)
      var Okeys = 0;
      var total = 0;
      for (var i = 0; i < summaryCopy.length; i++){
        for(var j = 0; j < summaryCopy[i].length; j++){
          if(summaryCopy[i][j] === "OK") {
            Okeys = Okeys + 1
          }
        }
        total = total + 1 
      }
      console.log(Okeys)
      console.log(total)
   }
}

Upvotes: 0

Related Questions