JavaScript Asynchronous Programming with JavaScript Understanding Promises Reject a Promise and Handle Errors

Amandeep Pasricha
Amandeep Pasricha
14,931 Points

What's the point of initializing a variable to true, if we already know what the outcome will be? (fulfilled)

As asked.

2 Answers

Tony Soukkeo
Tony Soukkeo
8,169 Points

It can be helpful, if you want to hide or display a certain element based on its Boolean value.

For example, on a webpage, I want to initially show an element to the user, so I would have a variable, let’s say showElement, set to true. And You can have a button, that if a user were to click on, it would set the showElement to false, thus hiding the element.

Robert O'Toole
Robert O'Toole
6,345 Points

i see here we are doing a test with simple examples like if (order){ resolve('string')}

in most cases what would the test condition look like?

i know with ajax its something like if(xhr.state==200&&xhr.readyState==4) to test if the request was sent ok. but in these cases im a bit confused?

Zoltán Erkel
Zoltán Erkel
6,328 Points

Same here so far. It's also somewhat irritating that (based on other comments from the community) one can't check the code as it can't be executed the same way as in the Visual Code terminal.

However based on the examples so far it seems like in contrast to AJAX readystate and state check, we for some reason provide the outcome. Sure this will be cleared along the way though.

UPDATE: to be fair it's probably just easier to model how promises work with passing a boolean to it