Skip to content
Advertisement

How does variable setting work with await?

Can someone explain to me why this is not working the way I am expecting?

I am expecting the last console.log to run after my functions run, but it is returning empty length string instead of the actual date.

These are the variables I want to set after my function call. Declaring them now so the scope is set globally.

JavaScript

This function gets my json data. I have URL declared above in my code, its returning everything as expected.

JavaScript

The function that is called above:

JavaScript

calling the function, so my variables should be set correctly after this function runs

JavaScript

I’m thinking this is a scope issue but I’m not sure why. Here is an example that I was testing the scope out on. This is how I was expecting my code to run:

JavaScript
JavaScript

Advertisement

Answer

You are not awaiting the call. The example code should have a promise in it.

JavaScript
JavaScript

Now with it awaiting the call

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement