Using variables outside the Fetch

0

I'm using the following Fetch:

var bdresultados;
if(respuesta.context.bueno){

      fetch('/bd/').then(response =>{

        return response.json();
        }).then(data => {
          for(let indice of data){
        bdresultados = indice.Personas;
          }
        }).catch(err => {
      });   
      console.log(bdresultados);
    }

The problem is that I need to use that variable var bdresultados outside the fetch. How could I do it? Thanks

    
asked by Isaac Alejandro 19.12.2018 в 01:08
source

1 answer

0

Declare it outside the fetch and then reaaccionas.

var bdresultados;
fetch('/bd/').then(response =>{
        return response.json();
        }).then(data => {
          for(let indice of data){
           bdresultados = indice.Personas;
          }
        }).catch(err => {
      });
    }

This way you can call it from outside. Take that as an example only because you must assign it at the appropriate level of the function.

    
answered by 19.12.2018 в 01:19