what's up y'all this is Hussein Nasser from I geometry podcast good morning
everybody it's 5:30 in the morning need work today's topic as a synchronous
versus asynchronous operations yeah so in order to talk about these two
operations or methods or style really of programming we need to understand we
need to understand how the basic operations work so I'm writing a program
today right writing a code any language JavaScript c-sharp and let's take this
program for it for instance you have a variable called a a equal 1 and then B
equal to but I have another variable C they called a plus B so it will happen
here as as long as the statement or the program is concerned what happened here
is each statement is getting executed in sequence right so a first look at the
value of 1 then B will get a value of 2 then C will get the value of a plus B
which is will be sent to the CPU to do the processing of adding the computation
of adding 1 plus 2 and then we'll return the results to see
I started to see so we'd understand that the third statement here the rest of the
program had to wait for that operation to complete in order to process these
values into the sea and then continuing with the rest of the program so this is
the traditional style of doing things which is synchronous right it's
synchronous wages sequentially execute wait for your results before continuing
right so this might be almost negligible if you think about it right because
adding a plus B it's just a small computational overhead
it's not even intolerant but so waiting is no really it's not really concern
here waiting for that result however there are other operations that
we execute and that these operations are more expensive to to block the user from
doing anything we'll just wait you know and you can think of few of these
examples like read or write to the desk right querying something on the network
accessing them in other v accessing the internet or pieces of information so
these operations usually takes
you know a few more milliseconds the mall even more sometimes it depends on
what you're done right so if you decided to write your application in a
synchronous way the user will feel it says oh why is your application slow
wise okay I click this button I understand that there consequences and
I'm willing to wait but let me do something else this is the track for the
asynchronous operations and this is coming up so in the previous segment we
discussed synchronous operations where the program literally blocked while
waiting for a result from single operation so this is a synchronous
operation so now stop everything you're doing all right suppose stop everything
you're doing let me execute that wait for that result because that result
we're gonna use it to do something else and that's the trick that's that
actually the good part of synchronous thing it's always it's always depending
on something else then if I'm doing something I'm gonna use it to do to take
that value of C in our example a plus B equals C we're gonna take C enter / -
for example alright so this is like the synchronous operation the sequence
really better right so however asynchronous now let's go
discuss asynchronous operations a secret operation says okay I know that this
function that I'm about to execute we're gonna take time and it depends sometimes
our gonna take few milliseconds sometimes it's gonna take more than a
second depending on the mood all right because if I am riding to the desk if
the disk is fragmented right it's gonna take longer if I am it's pilly if I'm
reading actually a breeding piece of file that files fragmented I have to go
then the disk i/o has to go and pull these pieces together and compiled a
file for creating file for you then return it for you so that that's really
expensive operation if you think about it especially if you do it over and over
and over again so you have to design your program if you want to do though
asynchronous and that's example HR JavaScript almost supports always
asynchronous stuff to you but to do activate I don't know so it's like okay
okay I have the file now I have this operation go and execute that and hey
whatever you ready call this function and tell me that you're ready or do
sometimes it's called delegate in c-sharp
sometimes it's goal Colback functions and got us good mostly but so do your
thing better return whatever your return just
like a think think of it like a like a bell right so you send your you send
someone to do some tasks and say hey whatever you come back just stronger
this bill let me know that you're you're back rid of that that particle of Milman
that bill is actually the function call back
now in our example if I want to convert my my application that a plus B we'll
see into an asynchronous application what are we gonna do is I'm gonna write
a function called sum right that's one way to do it then that takes a plus B
and then what does the computation and then what we will do is that sum
function will take actually three things the a the B and the third parameter is
actually the callback function which function should I call with the result
we're not ready so that's the callback function so you called the sum it B and
then they call that function and you go on your merry way so what do you do as
an A plus B call that function and then you continue that program is completely
open the user can do whatever she wants right click on other buttons do thanks
whatever the value of C is computed that function will be called
and guess what you'll be ready for it because you have already written code
for it that's sometimes called an event like a click event happen or something
happened right it's like unready or or completed and that completes our second
segment thank you guys for listening see in the next one now that we discussed
sync versus async operation now it's time to see what other advantages and
disadvantages of each operation
where he styled this in the synchronous although the advantages is clear coding
right you can clearly see that I added a plus B and I store the value in C very
clear the program is read from up from top to bottom but very simple clear to
the point and disadvantages slow operations like reading from this
reading file from this reading and rest operations but I call them I call the
method on the rest api from rest api that takes times in a wait for that to
return order to continue in order to unblock your user interface in order to
do other things and swap alright so simple coding kinda bad i would say bad
but it's not optimal user experience now async
they sing for asynchronous the advantages is a I call a method I don't
care how long it takes all I do I gotta take forever but I still can do other
thing in my application while calling that method so function calls to read
the disk read the file read the rest API read any method in the network will get
executed and then it will return as a callback function and the one over
thready I'll be ready for it I'll do my thing and my callback function my
callback function has for example friend C or take the value C and and divided by
2 and do something else but probably this is good as explaining an applause
or something so what gets to be really high all right
so disadvantage of a synchronous asynchronous code that disadvantages is
everybody agrees on there's this readability of again it is tough to read
the code so if you do C is equal a plus B or our APIs be some a plus B call back
if I don't do something with the see the rest of the code below that will get
executed anyway so you have to be very careful because it's like we'll just hey
make the call and then just continue executing the code code below that
statement so that can actually lead to tricky programming so you need to be
really really know what you're doing because you know if you can't read it
synchronously anymore you have to jump through different functions especially
if reading something else really
really hard to debug asynchronous code it's really challenging javascript
actually and other languages as well took care of this slightly so you still
can have an asynchronous code yet it feels like it's synchronous by using
promises and hey do that and I promise you - I'll call this function or I've
done but it's done in a very neat way so you can just it's very it's very
readable it's even called using the keywords saying async and awake so
you'll kill a function called async and then await that result although is you
sit a wait but it's actually execute in the background but you still have the
code for you to do whatever you want all right guys stay awesome see you on the
next episode take
you