Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Amandeep Pasricha
Amandeep Pasricha
14,932 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,170 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,352 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?

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