/    Sign up×
Community /Pin to ProfileBookmark

No returns from the fetch function?

I am currently a JavaScript Padawan learner.

After a few hours of trying It would seem that it is not possible to get a value or JSON object returned from a fetch function. Apparently I missed that in the MDN documentation on fetch.

“`
alert(site_rules());
function site_rules(){
fetch(“../conf/rules.json”, {
mode: ‘no-cors’,
cache: ‘no-cache’,
method: “POST”
})
.then(function (response) {
return response.json()})
.then(function (rules) {
return JSON.stringify(rules);
}).catch(function (error) {
console.log(error)
});
}
“`

For me it seems odd to retrieve a JSON file from the server and then be limited to using it only inside the fetch function.

Any insights?

Have a fantastic evening.
W.

to post a comment
Full-stack DeveloperJavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 22.2022 — >For me it seems odd to retrieve a JSON file from the server and then be limited to using it only inside the fetch function.

Yes, in the beginning it seems to be odd and annoying but you didn't miss anything: There is no way around.

Fetching data by use of Ajax is not the only situation where the procedure is asynchronous: It's the same when loading images, e. g. for use in a canvas or when using width and height: You need to wait until loading is finished.

Regarding your code I recommend to place all the processing that's based on data from the server inside a funktion and call it in the then-callback:
``<i>
</i>function procRules(rules) {
console.log(rules);
// All processing that accesses rules here
}
function site_rules(){
fetch("../conf/rules.json", {
mode: 'no-cors',
cache: 'no-cache',
method: "POST"
})
.then(function (response) {
return response.json()})
.then(function (rules) {
procRules(rules);
}).catch(function (error) {
console.log(error)
});
}<i>
</i>
``
Copy linkTweet thisAlerts:
@Programming_PadawanauthorJan 23.2022 — Thanks. I am still getting accustom to the asynchronous nature of JavaScript. I'm getting quicker at remembering it when I am trying to sort out something that isn't working correctly. Your response made me realize why it won't do a return from a fetch function. I did end up sticking the function needing the data in a .then() function passing in the JSON data.

I'm getting there and I'm learning a lot.





×

Success!

Help @Programming_Padawan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.11,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...