Skip to content Skip to sidebar Skip to footer

How Is This Specific ReactJs Code Executed Beginner Question?

I'm a beginner and are reading lots of code and now I wonder about the below code I understand what this code is doing what I need clarification on is the code flow. I see images l

Solution 1:

PlaceholderImages().then(images => {...

The first thing entering your stack here is the method PlaceholderImages. Thus, you should look at its code.

You will see it returns a new Promise that takes two arguments: 2 callback functions, one in case of success (resolve) and the other in case of failure (reject) of your API request. The API request happens when your perform a HTTP Request with the method GET using axios (a 3rd party library).

Again, you have an asynchronous function and the .then() method is only invoked once axos.get() returns something. If it never returns anything, you will never execute the then() function.

At this point, your stack (LIFO service discipline) will have the following methods to execute:

PlaceholderImages() -> anonymous function()* -> new Promise() -> axios.get();

PlaceholderImages() won't be executed until all the other methods are executed. If the axios.get() method fails to gather the desired data, once you are back to the PlaceholderImages().then() part of the code, you won't have any images to render. Thus, nothing will be rendered anyway.


Post a Comment for "How Is This Specific ReactJs Code Executed Beginner Question?"