1

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
Oresztesz
  • 2,294
  • 1
  • 15
  • 26

1 Answers1

1

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); 
});
barshopen
  • 1,190
  • 2
  • 15
  • 28
Oresztesz
  • 2,294
  • 1
  • 15
  • 26