Asynchronous vs synchronous

Kati Frantz
A free video tutorial from Kati Frantz
Full stack software engineer
4.5 instructor rating • 6 courses • 62,821 students

Learn more from the full course

Build NodeJS applications with Mongodb

Learn to build fullstack web applications with the most popular server side framework, NodeJS

04:18:10 of on-demand video • Updated September 2018

  • Build high quality applications built with Node, Express and MongoDB
  • Storing data with Mongodb database
  • Use cutting-edge ES6/ES7 JavaScript
  • Create Express web servers
  • Learn user sessions and authentication in nodejs applications
  • Communicating with a mongodb database using mongoose ODM
English [Auto] All right guys let's talk about a topic that is really important in the New Jersey community and that's a Synchronoss and Synchronoss processes. Now take a look at this picture. Ph.D. is an example of a normal back and programming language. Non-jazz is the new one which are talking about right now. So we call this exec each in ph B is a task is submitted. For example let's say I wanted to see the About page. So that task is submitted. And then the first thing ph does is understand the request or perform specific tasks. Let's say Abalos of fire and then saves a record into the database and then makes another API request to another server but it has a lot of task before it actually is done and then sends a response to the browser. Right. So the way Peachtree does it is it it performs tasks synchronously which means one task has to complete before the next task is done and then data completes before the next task is done. All right. So that's how ph does it but not just gives the possibility of performing tasks synchronously y. So look at what happens when a request is made to just automatically know just exit those task in parallel which means asynchronously. So rather than waiting for want us to complete before executing the order just like in P-H be no jazz records all of those tasks are exactly the same time. Which means that no task is blocking the order. Which brings us to a term called blocking and nonblocking code. OK. So for some of the some of this and we'll go to the example we have right now and make you understand. So in the index the fire the first thing we did was import these two. And then we actually use the referrers sync function to read this far form from this extremophiles. And then we went ahead to create our server and then respond to the specific request that the user is making right now. Notice that we used Arete 5 sync function and any function that you see Enoggera that ends with sync means that it is synchronous which means that it's going to block the execution of the code. Remember if it's Synchronoss that task must complete before it goes to the next task. And if it's a Synchronoss that US is going to be executor and all us are also going to be executor. Why that task is to executed. Right. So in this case we used to a function that has sync at the end which which means that this line of code will not be executed onto this one is done. And this one will not be executed on two. This one is also done. Which means the code is synchronous. OK. Now for the synchronous version this is going to have a function called Read far no sync at the end. So if we put fire right now and also we're going to put wheat flour right here you're going to see that if we execute this fire right now no it is going to give us a warning. So I'm just going to say not in jest. And look at that we have three warnings. We have Collignon and cannot function without Colback is deprecated. And it does that three times because we are clean we as Synchronoss functions right here. Now what does not just mean by that. No Jess is going to be executing this I synchronously which means it's going to start this line of code. And why are these refire function is to redesign this fire. It's going to start this line of code also. So this is not yet done. Doing what is supposed to do. But this has already started. And this one is also going to start and not just is also going to go to this one. OK. So look what happens not just tax. All of those tasks are exactly the same time. And then once everything is done it can now respond to the server but not just gives us a warning because what happens when those tasks are completed we need to know just what to do. OK so what we are going to just do is exec a function. So the second Parminter into or as synchronous functions is a function. OK. And most of the time for example the function receives the results of the Synchronoss process. For example this reform process is going to give us the file that has been read. OK. So after that just completely reached this far it's going to code this function we're passing in the fire that was read right here. OK so now that we've passed in the function as a second parameter into this with our function if we stop our server and started again you can see that we have two error messages right now which is for this process and this one. So not just expect that we pass in a function which is going to be executed after the Synchronoss process is completed. OK. Now why do we start by using synchronous functions. Because we don't want the server to respond when the page has not yet been read. OK because remember this task queue process is on the server and the server is going to take a few milliseconds more to read these files. So that's why I know just presents us with the option of using asynchronous processes. OK. So not is going to execute our code it to wait for one line to execute before it goes to the next line which means that not just the possibility of writing known blocking code. OK. So this line is not a block to this one and this one is not a block to this one but in this case this is Synchronoss which means they are blockbuster each other which means sued about pitches done within. It's not going to read the contact page until the contact pitches done with it. It's not going to read the homepage. So this is blocking code. But if you want to use the asynchronous version you are going to have to write nonblocking code. OK. Which is awesome. But we're going to see exactly how this is advantageous in future. But for now we're just going to leave it Synchronoss so that we know that before it acts like this line of code all the pages have already been fetched or read from this SJM affairs. All right guys. So that's your basic introduction to synchronous and asynchronous code in no chess are catching on in the very next lesson.