0

Duplicate of

How do I return the response from an asynchronous call?

Sorry all for duplicate and still thanks for responding!

  • Don't use that `json` variable outside of the `then`. You have to be async all the way down. – zero298 Oct 02 '17 at 20:06
  • Use callback fn? – Luka Čelebić Oct 02 '17 at 20:07
  • Use await/async? – Igor Soloydenko Oct 02 '17 at 20:07
  • 4
    welcome to asynchronous programming. You are trying to eat a delivery pizza before it gets to your house. – epascarello Oct 02 '17 at 20:08
  • The first one works because you're artificially including a delay (the time it takes you to paste the code), which allows the asynchronous operation to complete. – David Oct 02 '17 at 20:09
  • In your first example your kind of creating an artificial delay, if you had done a setTimeout it would have done the same. In the second example, you fetch & parseInt are in essence going to run at the same time.. – Keith Oct 02 '17 at 20:09
  • `So, got a wierd issue going on` not really.. :) – Keith Oct 02 '17 at 20:11
  • So what's going on here, is that nothing in the .then() calls is going to execute until api-call returns results. That code is registered as a 'callback' for when there's data, and won't be executed until then. In the meantime, everything after your catch is getting executed already. The 'json' variable hasn't been set yet, so is going to have value 'undefined'. – TinkerTank Oct 02 '17 at 20:11
  • "In your first example your kind of creating an artificial delay, if you had done a setTimeout it would have done the same. In the second example, you fetch & parseInt are in essence going to run at the same time.. – Keith 12 hours ago " Ah, I See! Will work on that. Thank you and all others for answering! – fosterfarms Oct 03 '17 at 08:30

0 Answers0