Reputation: 11
I'm trying to make a fetch function that not returns the data.
var url = "https://www.google.com/";
var testData = fetch(url, {
method:'GET'
})
.then(function(resp){
return resp.json();
})
.then(function(data){
return data;
});
console.log(testData); // here i want to print data
Upvotes: 1
Views: 751
Reputation: 2430
Fetch is returning a Promise<Response>
.You can't wait until promise finishes in the caller code. See more in this here. You can read more on promises here.
You can print the result in the last then
call:
var url = "https://www.google.com/";
fetch(url, {
method:'GET'
})
.then(function(resp){
return resp.json();
})
.then(function(data){
console.log(data);
});
Upvotes: 1