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 toinfo
are called as?– Sergio
The listJson is basically write the result in the gift.
– Steve Angello
If you show all the code I can give you an answer. Without more code I can’t answer...
– Sergio
I just want to use the fetch return on a global variable, the other functions I put in are just examples...
– Steve Angello
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 thatinfo
has the value offetch
but at first there is still... certain?– Sergio