0

I know this question has been answered many times but something isn't quite clicking in any of the explanations. I'm used to writing python so I'm pretty sure it's the async nature of promises that are eluding me.

I'm trying to make a simple get request and having a nightmare of a time. For some reason no matter now many return instances I instantiate, nothing seems to actually evaluate and "unwrap" the promise object I'm being returned from my fetch call. At this point I'm just trying to add more instances of return, await, or async because I'm clearly clueless as to what's going on under the hood. Thanks for your time.

async function get_data(url){
  return await fetch(url);
}

function RetreivedList() {
  const get_url = "https://km4mqt2a5d.execute-api.us-west-2.amazonaws.com/getList?URL=82ff68cce6444222be46ae1132d55cd8"; 
  console.log('making fetch...');    
  const foo = get_data(get_url).then(
    function(response) {
      return response.json()}).then(function(response){
    console.log(response.json())    
    return response;
      });


  console.log('heres the links!')

  console.log(foo);
  return(foo);
   };


console.log(RetreivedList());


RetreivedList().then(function(data){
  return(data.json())
});

codepen:https://codepen.io/dave-mitchell-the-looper/pen/JjYJjXq?editors=1112

user1949984
  • 93
  • 1
  • 6
  • For a start, you should call `.json()` exactly once. And you should place the `console.log` in the final callback - `RetreivedList().then(function(data){ console.log(data); })` – Bergi Apr 27 '20 at 16:36
  • still returns ```[object Promise] {}``` Why did you think that would work? – user1949984 Apr 27 '20 at 18:41
  • Huh? No. The `data` passed to the `then` callback can *never* be a promise. But `console.log(response.json())`, `console.log(foo)` and `console.log(RetreivedList())` are all expected to log a promise. Drop them. – Bergi Apr 27 '20 at 19:07
  • I don't get it. In all the examples I see, that's ALL that gets passed to then. Like this... First example, in ```ComponentDidMount()``` method https://reactjs.org/docs/faq-ajax.html – user1949984 Apr 27 '20 at 22:20

0 Answers0