WEBVTT 00:00.360 --> 00:06.930 This is the fourth video of this section callbacks in the previous video we looked at go routines in 00:06.930 --> 00:10.330 this video we'll write a function to convert a string to uppercase. 00:10.350 --> 00:14.450 We'll learn to use a synchronous and asynchronous callbacks within our routines. 00:14.520 --> 00:20.340 Now that we know how to use wait groups we can also introduce the concept of callbacks if you've ever 00:20.340 --> 00:23.730 worked with languages like javascript that used them extensively. 00:23.730 --> 00:25.630 This video will be familiar to you. 00:25.710 --> 00:30.750 A callback is an anonymous function that will be executed within the context of a different function. 00:30.750 --> 00:36.270 For example we want to write a function to convert a string to uppercase as well as making it asynchronous. 00:36.330 --> 00:39.310 How do we write this function so that we can work with callbacks. 00:39.330 --> 00:44.340 There's a little trick we can have a function that takes a string and returns a String let me show you 00:44.340 --> 00:44.990 this. 00:45.270 --> 00:46.850 Open the main dot go file. 00:47.580 --> 00:50.150 Here's the syntax of the code which you use. 00:50.280 --> 00:56.070 So take the returning type of this function which is a string and put it as the second parameter in 00:56.070 --> 00:58.260 an anonymous function as shown here. 00:58.260 --> 01:03.880 The next line of code which I'm about to show will allow you to do this let me add it. 01:03.940 --> 01:04.260 Here it is. 01:05.110 --> 01:11.410 Now these two up a sync function returns nothing but also takes a function that by coincidence also 01:11.410 --> 01:16.410 takes a string we can execute this function with the result we will usually return. 01:16.420 --> 01:19.420 Now let's add the same function with a little difference. 01:19.420 --> 01:25.570 Here we execute the F function with the result of calling the strings dot to up Smith with the provided 01:25.570 --> 01:28.570 word which returns the word parameter in uppercase. 01:28.660 --> 01:30.300 Let's write the main function too. 01:30.400 --> 01:35.750 Okay let me include the previous code as a comment which we used for explanation. 01:35.770 --> 01:38.250 Now it's done in our main code. 01:38.260 --> 01:42.230 We've identified our callback as you can see we passed the test. 01:42.230 --> 01:48.000 Hello callbacks to converted to uppercase next we pass the callback to be executed with the result of 01:48.000 --> 01:50.280 passing our string to uppercase. 01:50.280 --> 01:55.830 In this case we simply print the text in the console with the text callback in front of it it's time 01:55.830 --> 02:03.130 to execute this code and see what we get as result so save the file and navigate to the terminal run 02:03.130 --> 02:12.160 the command go run main dot go we get the output Hello callbacks strictly speaking this is a synchronous 02:12.160 --> 02:19.600 callback to make it asynchronous we have to introduce some concurrent handling let's do this go back 02:19.600 --> 02:25.190 to our code file here let's add the code for asynchronous callbacks. 02:25.360 --> 02:31.720 This is the same code executed asynchronously we use wait groups to handle concurrency we'll see later 02:31.720 --> 02:38.990 that channels can also be used to this now our function to upper async is as its name implies asynchronous 02:39.860 --> 02:44.540 we launched the callback in a different go routine by using the keyword go when calling the callback 02:45.620 --> 02:51.170 we write a small message to show the ordering nature of the concurrent execution more precisely we wait 02:51.170 --> 02:57.320 until the callback signals that it's finished and we can exit the program safely let's again execute 02:57.320 --> 03:04.920 the code and see the result move back to the terminal and run the main dot go file as you can see the 03:04.920 --> 03:10.620 program reaches the end of the main function before executing the callback in the two hour pacing function. 03:10.620 --> 03:17.330 This pattern brings many possibilities but leaves us open to one big problem called callback hell the 03:17.330 --> 03:22.890 term callback hell is commonly used to refer to when many callbacks have been stacked with each other. 03:23.090 --> 03:27.220 This makes them difficult to reason with and handle when they grow too much. 03:27.290 --> 03:32.480 For example using the same code as before we could stack another asynchronous call with the contents 03:32.480 --> 03:39.100 that we previously printed to the console let's open the main dot go file here. 03:39.170 --> 03:44.860 We need to make some changes we add another callback to the first up a sync function. 03:44.860 --> 03:51.890 Close the brackets so you've emitted imports the package name and the two upper async function as they 03:51.890 --> 03:53.440 have not changed. 03:53.480 --> 03:59.300 Now we have a two other async function within a two hour pacing function and we could embed many more 03:59.420 --> 04:00.700 if we want. 04:00.710 --> 04:06.080 In this case we again passed a text that we previously printed on the console to use it in the following 04:06.080 --> 04:14.360 callback the inner callback finally prints on the console time to check the output save the file and 04:14.360 --> 04:21.360 get back to a terminal execute the run command in this case we can assume that the outer callback will 04:21.360 --> 04:23.220 be executed before anyone. 04:23.280 --> 04:26.280 That's why we don't need to add one more to the weight group. 04:26.280 --> 04:31.470 The point here is that we must be careful when using callbacks in very complex systems too many callbacks 04:31.470 --> 04:38.300 are hard to reason with and how to deal with but with care and rationality they are powerful tools superb 04:38.840 --> 04:39.370 in this video. 04:39.380 --> 04:44.780 We dived into callbacks and they usage in the next video we'll be exploring about new taxes.