Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Ben Os
Ben Os
20,008 Points

Sync VS Async and XHRO question

Data items requested directly from the backend does require webpage bootstrapping...

In contrast, data items requested with XHRO as in AJAX (or AJAJ, to be more modern) are returned without bootstrapping of the webpage.

My question is this: Why excactly does the first way is synchronic and the second is asynchronic (by means of how the requested data items will return), I mean, why does the ABSENCE OF BOOTSTRAPPING necesses asyncasy?

Ben Os
Ben Os
20,008 Points

Due the complexity of the question, and as it is a direct continuation of another question I had in the following link here, I humbly tag (and will further avoid that), our beloved teacher Dave McFarland .

https://teamtreehouse.com/community/a-question-about-ajax-basics-course

1 Answer

Synchronous because if the data is requested on the server, is it sent to the client with the rest of the page (assuming that's what they are requesting). The client (typically a web browser), will parse the code and execute any scripts, in the order it appears, and only continue once they have finished, hence, synchronous. In this case, any script can use variables with values set by earlier code.

If part of that code contains an Ajax call or other type of asynchronous code, it will begin to execute that code (or send another HTTP request in the case of Ajax), but it will not block the execution of other code. This also means that if the async code is expected to return a value, you can't guarantee it will be available when other synchronous code runs.

This is typically handled with callbacks or promises, both which essentially 'wait' for a response and then execute some additional code when it receives it (or something else if there's an error).

Hope that helps.