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

Asynchronous and Synchronous Programming

I'd like to think I have a nice grasp on the concepts behind how asynchronous and synchronous flows work, but knowing async/sync code is another story (Unless ReactJS ES6).

It's come to that point where I needed to finally ask, as the answers I've found just aren't giving the answers I need. I recently ran across an issue with some NodeJS code where I was trying to return a value from a callback, and after doing some reading on the issue, it was said the code needed to be all async, or sync to be able to return from nested callbacks. I would love to confirm this, and of course understand how to do this myself. I thought that it was sync unless a promise came into play? Since callbacks are waiting for processes to finish, I've always thought of them to be synchronous since they were blocking, but at the same time running another call after my function runs before it's finished. I'm quite confused.

I've been through half of the full-stack track, and callbacks just seem to be common use, as well as thought the track was pretty much synchronous, hence the reason there was little discussion on it. My apologies if I missed something, and I'm wrong about this.

Thanks in advance, and my apologies for the long drawn-out question.

2 Answers

Ok, I didn't want to select my last answer for the "best answer", as it still wasn't a great answer for the issue I was having. What was happening with my code was that I was using a mix of synchronous, and asynchronous calls in a function, which is a big "no-no" with Node.JS. This is because Node.JS was created to be a non-blocking Asynchronous solution.

This confused me at first, as I had no idea how to differentiate between the two types, thinking it was based on the coding style. In a sense I guess it could be said to be, as you need to stay away from any synchronous calls that could stop the flow of the event loop.

To give a couple examples:

// Synchronous
console.log('Hello');
console.log('World');

The second console.log will not execute until after the first is done executing. It's "blocking" the event flow.

Another example but asynchronously:

// Asynchronous
setTimeout(function() {
  console.log('Hello');
}, 0);
setTimeout(function() {
  console.log('World');
}, 0);

Since the setTimeout function is asynchronous, both instances will run at the same time, and adding a higher timeout to the first instance will demostrate this by showing an output of 'World Hello'. This is considered "non-blocking", since the code is not stopping the event flow.

So I guess in a literal sense it could be due to style, but even more literally you can continue to use the object oriented style to code asynchronously. I personally prefer the functional style, but as they say, "There is no wrong way to do it"; as long as it's not synchronous.

Ok, I confused myself. I just now noticed that Node.JS, per their website, is a "non-blocking" (async) I/O framework, and not dependent on the code style. My apologies for taking up space.