Return fetch js result on a global variable

Asked

Viewed 272 times

0

Hi, sorry about the layman’s question.

Basically I want to use the fetch return and put in a variable, I can use it in other functions to make 1 request only, today I’m using const xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); xhr.open('GET', 'URL', true);

but following this structure below (someone tells me the name to study).

    let info = '';

    // Não sei o nome exato dessa estrutura de funções, ja pesquisei mas não achei
    // Eu vi assim uma vez e pra mim fica bonito e organizado rs

    const product = {
        //let info = ''; **Não sei como definir variável aqui dentro**

        getJson: function(){
            fetch('urlToFetch')
            .then(response => response.json()) 
            .then(result => {console.log(result)})
            .catch(err => {console.error('Failed retrieving information', err)})
        info = result
        return info
        },

        listJson: function(){
            // Quero usar as infos da variável 'info' aqui
        },

        changeJson: function(){
            // Quero usar as infos da variável 'info' aqui 
        },

        init: function(){
            product.getProduct()
        }
    }
  • When are you gonna use listJson? I mean, can you show the code or logic that your application has? Those methods that need to info are called as?

  • The listJson is basically write the result in the gift.

  • If you show all the code I can give you an answer. Without more code I can’t answer...

  • I just want to use the fetch return on a global variable, the other functions I put in are just examples...

  • So put the info = result within the .then( and the return of fetch will be available in this global variable... the problem (and I think that’s what you want to solve) is that other functions run counting that info has the value of fetch but at first there is still... certain?

1 answer

-1

The variable products is a javascript object. Javascript object contains only key and value.

const products = {
  chave1: 'valor1',
  chave2: 'valor2'
}

In this case, the value of the keys can be string number or more complex objects as a function for example:

const products = {
  chave1: function () {
            return 'valor1';
          },
  chave2: function () {
            return 'valor2';
          },
}

But since const products is an object it accepts only key and value. It is not possible to declare variable directly on the object as you did.

const products = {
  let variavel = ''; // ERRADO
  chave1: function () {
            return 'valor1';
          },
  chave2: function () {
            return 'valor2';
          },
}

You can declare another key to be used as variable if that is your intention

const products = {
  variavel: '', // CERTO
  chave1: function () {
            return 'valor1';
          },
  chave2: function () {
            return 'valor2';
          },
}

But inside the function keys has free use of variable

const products = {
  variavel: '', // CERTO
  chave1: function () {
            const valor = 'valor1'; // CORRETO
            return valor1;
          },
  chave2: function () {
            return 'valor2';
          },
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.