How can I acces the values of my async fetch function? [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
I want to use my fetched values in another function
I'm really new to JS. So until now I tried this.setState() and a return value of the function .
async fetchData() {
const url = 'http://localhost:8080';
const response = await fetch(url);
const data = await response.json();
// stringify JSON
var myJSON = JSON.stringify(data);
// parsing to JS object
var jsonData = JSON.parse(myJSON);
}
until now, I'm getting a Promise with the status "pending" . How do I get the actual value?
javascript async-await fetch
marked as duplicate by Matthew Herbst, Amadan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 7:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
I want to use my fetched values in another function
I'm really new to JS. So until now I tried this.setState() and a return value of the function .
async fetchData() {
const url = 'http://localhost:8080';
const response = await fetch(url);
const data = await response.json();
// stringify JSON
var myJSON = JSON.stringify(data);
// parsing to JS object
var jsonData = JSON.parse(myJSON);
}
until now, I'm getting a Promise with the status "pending" . How do I get the actual value?
javascript async-await fetch
marked as duplicate by Matthew Herbst, Amadan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 7:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Anasyncfunction can only ever return a promise. UsefetchData().then(...), orawait fetchData()(if you addreturn data). Or use it directly there, insidefetchData. (Also, note thatdatais already parsed data, the last two lines after it are useless.)
– Amadan
Jan 4 at 7:48
2
datais an Object. Why would you turn it into a JSON string only to parse it right back into an object? Just usedata. Anyway, this function doesn't return anything, and it doesn't callsetState(). Please add all relevant code to your question.
– Chris G
Jan 4 at 7:48
add a comment |
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
I want to use my fetched values in another function
I'm really new to JS. So until now I tried this.setState() and a return value of the function .
async fetchData() {
const url = 'http://localhost:8080';
const response = await fetch(url);
const data = await response.json();
// stringify JSON
var myJSON = JSON.stringify(data);
// parsing to JS object
var jsonData = JSON.parse(myJSON);
}
until now, I'm getting a Promise with the status "pending" . How do I get the actual value?
javascript async-await fetch
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
I want to use my fetched values in another function
I'm really new to JS. So until now I tried this.setState() and a return value of the function .
async fetchData() {
const url = 'http://localhost:8080';
const response = await fetch(url);
const data = await response.json();
// stringify JSON
var myJSON = JSON.stringify(data);
// parsing to JS object
var jsonData = JSON.parse(myJSON);
}
until now, I'm getting a Promise with the status "pending" . How do I get the actual value?
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
javascript async-await fetch
javascript async-await fetch
asked Jan 4 at 7:43
KurgerBingKurgerBing
418
418
marked as duplicate by Matthew Herbst, Amadan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 7:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Matthew Herbst, Amadan
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 7:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Anasyncfunction can only ever return a promise. UsefetchData().then(...), orawait fetchData()(if you addreturn data). Or use it directly there, insidefetchData. (Also, note thatdatais already parsed data, the last two lines after it are useless.)
– Amadan
Jan 4 at 7:48
2
datais an Object. Why would you turn it into a JSON string only to parse it right back into an object? Just usedata. Anyway, this function doesn't return anything, and it doesn't callsetState(). Please add all relevant code to your question.
– Chris G
Jan 4 at 7:48
add a comment |
Anasyncfunction can only ever return a promise. UsefetchData().then(...), orawait fetchData()(if you addreturn data). Or use it directly there, insidefetchData. (Also, note thatdatais already parsed data, the last two lines after it are useless.)
– Amadan
Jan 4 at 7:48
2
datais an Object. Why would you turn it into a JSON string only to parse it right back into an object? Just usedata. Anyway, this function doesn't return anything, and it doesn't callsetState(). Please add all relevant code to your question.
– Chris G
Jan 4 at 7:48
An
async function can only ever return a promise. Use fetchData().then(...), or await fetchData() (if you add return data). Or use it directly there, inside fetchData. (Also, note that data is already parsed data, the last two lines after it are useless.)– Amadan
Jan 4 at 7:48
An
async function can only ever return a promise. Use fetchData().then(...), or await fetchData() (if you add return data). Or use it directly there, inside fetchData. (Also, note that data is already parsed data, the last two lines after it are useless.)– Amadan
Jan 4 at 7:48
2
2
data is an Object. Why would you turn it into a JSON string only to parse it right back into an object? Just use data. Anyway, this function doesn't return anything, and it doesn't call setState(). Please add all relevant code to your question.– Chris G
Jan 4 at 7:48
data is an Object. Why would you turn it into a JSON string only to parse it right back into an object? Just use data. Anyway, this function doesn't return anything, and it doesn't call setState(). Please add all relevant code to your question.– Chris G
Jan 4 at 7:48
add a comment |
1 Answer
1
active
oldest
votes
When you mark the function as async that implicitly wraps any return value it has with a Promise. You're not actually returning anything, so fetchData will just return a Promise that resolves to undefined.
So first, you need to actually return something from your function:
async fetchData() {
const url = 'http://localhost:8080';
const response = await fetch(url);
const data = await response.json();
return data; // It should already be parsed JSON since you called `response.json()`
}
Then you need to wait for the Promise to complete in the calling function:
// You can use async/await
async someOtherFunction() {
const value = await fetchData();
// Do things...
}
// Or use .then
someOtherFunction() {
fetchData().then((value) => {
// Do things...
});
}
Good answer! Just one thing to add: there's no need to putawaitbeforeresponse.json(), since that's not an asynchronous operation
– Patrick Hund
Jan 4 at 7:53
@PatrickHund it absolutely is: developer.mozilla.org/en-US/docs/Web/API/Body/json
– Matthew Herbst
Jan 4 at 7:55
Oh, OK, I stand corrected
– Patrick Hund
Jan 4 at 7:55
Also this link developer.mozilla.org/en-US/docs/Web/API/Fetch_API can be useful to anyone who need complete documentation, maybe you should add it to your answer ?
– JeuneApprenti
Jan 4 at 7:59
1
ok, I got it! I used your advices, but instead of getting out the return I build in my logic after theawait fetchData(). Now I can use the values without problems. Thanks again for your patience :D
– KurgerBing
Jan 4 at 11:12
|
show 4 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you mark the function as async that implicitly wraps any return value it has with a Promise. You're not actually returning anything, so fetchData will just return a Promise that resolves to undefined.
So first, you need to actually return something from your function:
async fetchData() {
const url = 'http://localhost:8080';
const response = await fetch(url);
const data = await response.json();
return data; // It should already be parsed JSON since you called `response.json()`
}
Then you need to wait for the Promise to complete in the calling function:
// You can use async/await
async someOtherFunction() {
const value = await fetchData();
// Do things...
}
// Or use .then
someOtherFunction() {
fetchData().then((value) => {
// Do things...
});
}
Good answer! Just one thing to add: there's no need to putawaitbeforeresponse.json(), since that's not an asynchronous operation
– Patrick Hund
Jan 4 at 7:53
@PatrickHund it absolutely is: developer.mozilla.org/en-US/docs/Web/API/Body/json
– Matthew Herbst
Jan 4 at 7:55
Oh, OK, I stand corrected
– Patrick Hund
Jan 4 at 7:55
Also this link developer.mozilla.org/en-US/docs/Web/API/Fetch_API can be useful to anyone who need complete documentation, maybe you should add it to your answer ?
– JeuneApprenti
Jan 4 at 7:59
1
ok, I got it! I used your advices, but instead of getting out the return I build in my logic after theawait fetchData(). Now I can use the values without problems. Thanks again for your patience :D
– KurgerBing
Jan 4 at 11:12
|
show 4 more comments
When you mark the function as async that implicitly wraps any return value it has with a Promise. You're not actually returning anything, so fetchData will just return a Promise that resolves to undefined.
So first, you need to actually return something from your function:
async fetchData() {
const url = 'http://localhost:8080';
const response = await fetch(url);
const data = await response.json();
return data; // It should already be parsed JSON since you called `response.json()`
}
Then you need to wait for the Promise to complete in the calling function:
// You can use async/await
async someOtherFunction() {
const value = await fetchData();
// Do things...
}
// Or use .then
someOtherFunction() {
fetchData().then((value) => {
// Do things...
});
}
Good answer! Just one thing to add: there's no need to putawaitbeforeresponse.json(), since that's not an asynchronous operation
– Patrick Hund
Jan 4 at 7:53
@PatrickHund it absolutely is: developer.mozilla.org/en-US/docs/Web/API/Body/json
– Matthew Herbst
Jan 4 at 7:55
Oh, OK, I stand corrected
– Patrick Hund
Jan 4 at 7:55
Also this link developer.mozilla.org/en-US/docs/Web/API/Fetch_API can be useful to anyone who need complete documentation, maybe you should add it to your answer ?
– JeuneApprenti
Jan 4 at 7:59
1
ok, I got it! I used your advices, but instead of getting out the return I build in my logic after theawait fetchData(). Now I can use the values without problems. Thanks again for your patience :D
– KurgerBing
Jan 4 at 11:12
|
show 4 more comments
When you mark the function as async that implicitly wraps any return value it has with a Promise. You're not actually returning anything, so fetchData will just return a Promise that resolves to undefined.
So first, you need to actually return something from your function:
async fetchData() {
const url = 'http://localhost:8080';
const response = await fetch(url);
const data = await response.json();
return data; // It should already be parsed JSON since you called `response.json()`
}
Then you need to wait for the Promise to complete in the calling function:
// You can use async/await
async someOtherFunction() {
const value = await fetchData();
// Do things...
}
// Or use .then
someOtherFunction() {
fetchData().then((value) => {
// Do things...
});
}
When you mark the function as async that implicitly wraps any return value it has with a Promise. You're not actually returning anything, so fetchData will just return a Promise that resolves to undefined.
So first, you need to actually return something from your function:
async fetchData() {
const url = 'http://localhost:8080';
const response = await fetch(url);
const data = await response.json();
return data; // It should already be parsed JSON since you called `response.json()`
}
Then you need to wait for the Promise to complete in the calling function:
// You can use async/await
async someOtherFunction() {
const value = await fetchData();
// Do things...
}
// Or use .then
someOtherFunction() {
fetchData().then((value) => {
// Do things...
});
}
answered Jan 4 at 7:49
Matthew HerbstMatthew Herbst
11.4k134890
11.4k134890
Good answer! Just one thing to add: there's no need to putawaitbeforeresponse.json(), since that's not an asynchronous operation
– Patrick Hund
Jan 4 at 7:53
@PatrickHund it absolutely is: developer.mozilla.org/en-US/docs/Web/API/Body/json
– Matthew Herbst
Jan 4 at 7:55
Oh, OK, I stand corrected
– Patrick Hund
Jan 4 at 7:55
Also this link developer.mozilla.org/en-US/docs/Web/API/Fetch_API can be useful to anyone who need complete documentation, maybe you should add it to your answer ?
– JeuneApprenti
Jan 4 at 7:59
1
ok, I got it! I used your advices, but instead of getting out the return I build in my logic after theawait fetchData(). Now I can use the values without problems. Thanks again for your patience :D
– KurgerBing
Jan 4 at 11:12
|
show 4 more comments
Good answer! Just one thing to add: there's no need to putawaitbeforeresponse.json(), since that's not an asynchronous operation
– Patrick Hund
Jan 4 at 7:53
@PatrickHund it absolutely is: developer.mozilla.org/en-US/docs/Web/API/Body/json
– Matthew Herbst
Jan 4 at 7:55
Oh, OK, I stand corrected
– Patrick Hund
Jan 4 at 7:55
Also this link developer.mozilla.org/en-US/docs/Web/API/Fetch_API can be useful to anyone who need complete documentation, maybe you should add it to your answer ?
– JeuneApprenti
Jan 4 at 7:59
1
ok, I got it! I used your advices, but instead of getting out the return I build in my logic after theawait fetchData(). Now I can use the values without problems. Thanks again for your patience :D
– KurgerBing
Jan 4 at 11:12
Good answer! Just one thing to add: there's no need to put
await before response.json(), since that's not an asynchronous operation– Patrick Hund
Jan 4 at 7:53
Good answer! Just one thing to add: there's no need to put
await before response.json(), since that's not an asynchronous operation– Patrick Hund
Jan 4 at 7:53
@PatrickHund it absolutely is: developer.mozilla.org/en-US/docs/Web/API/Body/json
– Matthew Herbst
Jan 4 at 7:55
@PatrickHund it absolutely is: developer.mozilla.org/en-US/docs/Web/API/Body/json
– Matthew Herbst
Jan 4 at 7:55
Oh, OK, I stand corrected
– Patrick Hund
Jan 4 at 7:55
Oh, OK, I stand corrected
– Patrick Hund
Jan 4 at 7:55
Also this link developer.mozilla.org/en-US/docs/Web/API/Fetch_API can be useful to anyone who need complete documentation, maybe you should add it to your answer ?
– JeuneApprenti
Jan 4 at 7:59
Also this link developer.mozilla.org/en-US/docs/Web/API/Fetch_API can be useful to anyone who need complete documentation, maybe you should add it to your answer ?
– JeuneApprenti
Jan 4 at 7:59
1
1
ok, I got it! I used your advices, but instead of getting out the return I build in my logic after the
await fetchData() . Now I can use the values without problems. Thanks again for your patience :D– KurgerBing
Jan 4 at 11:12
ok, I got it! I used your advices, but instead of getting out the return I build in my logic after the
await fetchData() . Now I can use the values without problems. Thanks again for your patience :D– KurgerBing
Jan 4 at 11:12
|
show 4 more comments
An
asyncfunction can only ever return a promise. UsefetchData().then(...), orawait fetchData()(if you addreturn data). Or use it directly there, insidefetchData. (Also, note thatdatais already parsed data, the last two lines after it are useless.)– Amadan
Jan 4 at 7:48
2
datais an Object. Why would you turn it into a JSON string only to parse it right back into an object? Just usedata. Anyway, this function doesn't return anything, and it doesn't callsetState(). Please add all relevant code to your question.– Chris G
Jan 4 at 7:48