Answer the question
In order to leave comments, you need to log in
How to call FETCH once?
Hello.
There is a request in which the data comes.
How and where is it better to write them down so that they can be used in other functions?
function func() {
fetch('bla-bla-bla')
.then(data => data.json())
.then(data => data)
}
Answer the question
In order to leave comments, you need to log in
Declare a variable outside the function and put the result of the fetch in it.
const func = (() => {
const url = 'bla-bla-bla';
let result;
return async () => {
if (typeof result == 'undefined') {
result = await (() => {
return fetch(url).then(response => response.json());
})();
}
return result;
};
})();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question