This is the fourth video of this section callbacks in the previous video we looked at go routines in
this video we'll write a function to convert a string to uppercase.
We'll learn to use a synchronous and asynchronous callbacks within our routines.
Now that we know how to use wait groups we can also introduce the concept of callbacks if you've ever
worked with languages like javascript that used them extensively.
This video will be familiar to you.
A callback is an anonymous function that will be executed within the context of a different function.
For example we want to write a function to convert a string to uppercase as well as making it asynchronous.
How do we write this function so that we can work with callbacks.
There's a little trick we can have a function that takes a string and returns a String let me show you
this.
Open the main dot go file.
Here's the syntax of the code which you use.
So take the returning type of this function which is a string and put it as the second parameter in
an anonymous function as shown here.
The next line of code which I'm about to show will allow you to do this let me add it.
Here it is.
Now these two up a sync function returns nothing but also takes a function that by coincidence also
takes a string we can execute this function with the result we will usually return.
Now let's add the same function with a little difference.
Here we execute the F function with the result of calling the strings dot to up Smith with the provided
word which returns the word parameter in uppercase.
Let's write the main function too.
Okay let me include the previous code as a comment which we used for explanation.
Now it's done in our main code.
We've identified our callback as you can see we passed the test.
Hello callbacks to converted to uppercase next we pass the callback to be executed with the result of
passing our string to uppercase.
In this case we simply print the text in the console with the text callback in front of it it's time
to execute this code and see what we get as result so save the file and navigate to the terminal run
the command go run main dot go we get the output Hello callbacks strictly speaking this is a synchronous
callback to make it asynchronous we have to introduce some concurrent handling let's do this go back
to our code file here let's add the code for asynchronous callbacks.
This is the same code executed asynchronously we use wait groups to handle concurrency we'll see later
that channels can also be used to this now our function to upper async is as its name implies asynchronous
we launched the callback in a different go routine by using the keyword go when calling the callback
we write a small message to show the ordering nature of the concurrent execution more precisely we wait
until the callback signals that it's finished and we can exit the program safely let's again execute
the code and see the result move back to the terminal and run the main dot go file as you can see the
program reaches the end of the main function before executing the callback in the two hour pacing function.
This pattern brings many possibilities but leaves us open to one big problem called callback hell the
term callback hell is commonly used to refer to when many callbacks have been stacked with each other.
This makes them difficult to reason with and handle when they grow too much.
For example using the same code as before we could stack another asynchronous call with the contents
that we previously printed to the console let's open the main dot go file here.
We need to make some changes we add another callback to the first up a sync function.
Close the brackets so you've emitted imports the package name and the two upper async function as they
have not changed.
Now we have a two other async function within a two hour pacing function and we could embed many more
if we want.
In this case we again passed a text that we previously printed on the console to use it in the following
callback the inner callback finally prints on the console time to check the output save the file and
get back to a terminal execute the run command in this case we can assume that the outer callback will
be executed before anyone.
That's why we don't need to add one more to the weight group.
The point here is that we must be careful when using callbacks in very complex systems too many callbacks
are hard to reason with and how to deal with but with care and rationality they are powerful tools superb
in this video.
We dived into callbacks and they usage in the next video we'll be exploring about new taxes.