 Good evening, I am going to talk about how to get out of the call back hell, so it is going to be a sneak peak I am not going to cover everything and I intend to release a library during our PHP Conf Asia, so I am thinking of covering the entire thing there, but I will give you a sneak peak now, okay. So, what is call back hell? From visionary I picked up this, basically call backs are nested calls right, multiple calls nested inside other and it goes so deep and the code looks so ugly and it is very hard to manage that code. So, today's presentation I am going to focus on the difficult to understand and maintain portion, how to tackle that, okay. So, why we started using call backs? Because for sometimes certain things are not with us, we are waiting for something and instead of simply waiting if there is a call back, say you want to bake a pizza right, you put it in the oven and then you have to just sit and watch, if there is nothing else you can do, yeah you can do that, but you have other things to do, in that case what you will do, you will just let the oven alert you, like after setting the timer there can be some sound that is your call back, then you can go and attend to that, when your attention is not needed your time can be better spent elsewhere, so that is what we can do with call backs. So, with PHP what are all the things that take time, where we can do something else, disk reading and writing, writing a file, reading it from somewhere and network reading and writing, basically like you make a HTTP call and waiting for its response or anything of that sort and yeah talking to DB can be synchronous, sometimes like can take time, you make a query and the time the query results come back in between, if it is a call back you can do something else as well and then sending mail, just now we saw the vulnerabilities in that, right. So, if you have other things to do, we better use call backs, so that we are alerted when the thing is ready, then we can attend to it. So, in a single-threaded operation say for example, there is only one person in the shop and you are ordering a pizza and milkshake, the same person is supposed to make them and serve hot, I mean the pizza hot and the cold milkshake, they have to serve them fresh, if they are not doing it this way, what will happen, they will put the pizza in the oven and wait for it to bake, after that they will go and start making the yeah milkshake. So, because of this what will happen, you have to wait longer, right. So, if you can attend to them parallely, we can serve the customer quickly. So, let us talk about one of the use cases, right for call backs. In a synchronous situation, client makes a request to the server and then simply waits, the server computes, puts the things together, creates a response and then the response comes back and then the client is like processing that response. Whereas, in an asynchronous situation, we will simply make the request and we will continue to work on something else, say for example, here we will make the milkshake, while pizza is being baked, right. And then we get a response saying that the pizza is ready, then we will put them together in a tray and serve to the client, right. So, typically we will see them like this, right. Javascript, everything is like asynchronous, in PHP everything is synchronous, with PHP the convenience is like you can just write everything in a sequential manner, it will be exactly executed the same way. Once you go asynchronous, it is hard to predict, we do not know which one will be prepared first, right. Depending on various conditions, things can change, right, ok. But the thing is, that is a typical scenario before, PHP can do asynchronous operations, right. How it can do it? There are several built-in functions and using those built-in functions, people have built several libraries with which we are able to do asynchronous callback. And if you are using Facebook hack as well, you will be able to make asynchronous operations with hack, that would not be exactly considered PHP, but it originated from PHP. So, I put it there. I will give you one example. I am not going to list down all the different ways you can do asynchronous operations with PHP. One thing you can do with curl calls, there is a way you can do curl call asynchronously, right. There is a parameter to set, then you are, you can immediately do other stuff while the curl call is running in the background, right. So, that is one way. And as far as libraries are concerned, there is a framework called React PHP. The next presenter is going to talk about that, right, how to use React PHP to make asynchronous operations. So, this is the callback hell. I am presenting you an example of a blockchain call using PHP. So, what we need to do is very simple. We just connect to a blockchain and then get a list of accounts that are present there and then take two accounts and check their balance, how much of ether each account holds and then sum them up and print them together, right. For this, because these calls are actually internally using asynchronous curl, using gulp and all in the background. So, each and every call uses a callback. Basically, you want to get the account for that. It does not require any other parameter. So, you need to pass a callback parameter there where that function will be called with error and accounts. If there is an error, the error object will be populated, otherwise it will be null. Otherwise, when it is null, you will have the actual object that you are expecting. So, you have to check whether the operation completed successfully. So, first you check whether error is not null, then you print what is the error and then return, you do not continue from there, you stop there. Once this is successful, you are coming down here, then you start checking the balance for the first account, right. Because another thing is like with JavaScript, it has, you can refer to variables in the previous scope. So, you can easily access stuff within the total scope, right. A function within a function can access what is in the parent function, but here that is not the case. For PHP, you need to pass the reference using a use statement. So, I need to refer to the eth object and the accounts in order to continue and get the balance of the second account, ok. So, I need to pass them and then again do the same thing, whether I am getting the result or not and then I just print the balance for the first person. Then I make another call, right, inside this, that is where I actually get the second account's balance, then I total them up and print them, that is all, that is what it does. If this is a synchronous operation, how we would have handled it, it would have been very simple code like this, right. You just get the eth, get the accounts and then get the balance for the first account and then echo it out, get the balance for the second account, sum them up, print them, done. If at all there is an error, that is cost anywhere, you can just put a try statement. So, it will be catched and you will print the error message. So, this will exactly do the same thing that the previous one was doing, but unfortunately this is not synchronous. This is an asynchronous call and it is giving you only a call back, right. So, how can we solve this situation? So, I really hate writing code with the call backs kept inside and that nested code is very ugly, hard to maintain. So, I used to develop in different languages. The first language I saw asynchronous is C sharp, one of the dot net languages, where you are able to write code which is asynchronous, but certain things have to wait for previous operation to finish, right. There you can simply, so the way it works is like this. You put a async in front of the function and wherever you will just put an await, with that you can make the call, it will wait wherever required, get the result and finally, you can complete the operation. So, I was thinking how can we do that with PHP? I looked at all the possibilities. So, one feature that is available in PHP for quite some time, since PHP 5.6 is generator, right. Have you ever used generator? No, generator looks very much like a function. Let me show you a solution for this that I have developed, ok. So, first step what I do is like I need to put it inside a function. If I want to use async await in like in any other language, I have to put it inside a function that is what I am doing here, nothing else. The whole thing just put inside a function, I am calling it. So, now I will make it asynchronous. So, you see what has changed now, right. There is a statement called yield. If you put a yield in your function, that function becomes a generator. So, what this yield will do, right, it is like a break point. It would not execute the whole function. Wherever it finds an yield, it will wait, wait for you to ask it to continue. Till then it would not run the subsequent commands, right. So, that is the advantage of yield. So, I wrote a library that provides this async function. When you, this code looks very much like the synchronous code, right, except what I am doing here. So, wherever I need to wait, I need to wait in three places. I need to wait for the accounts and then I need to wait for the balance of the two accounts, right. So, there I am placing all the yield statements. You can see yield for the accounts and the balance, but I am not calling the functions directly, ok. In PHP, we have something called callable, right. What is callable? You can write an anonymous function, you can pass it, that is callable. Otherwise, you can pass a name of a function that is callable. You can immediately call that and get the result of that. Similarly, you can pass an object and its method as an array, right, that is callable. So, I took that syntax, right. If you see this is exactly callable, this array is callable. I can call it like a function and you will get the result. I am extending it further because for some functions, you need to pass parameters. Say for example, getBalance requires the account address to be passed as a parameter. So, I am just extending that. So, I just put it in an array, eth getBalance accounts. So, this is the method I want to call. And then the result of this should be stored in this variable, ok. So, with that, I get the value and this exactly works the way we would expect. It gets the result. So, async takes a generator object. Defining a generator in PHP is very simple. If you want to define a generator in JavaScript, you have to do two things. You need to put yield and then with the function, you need to put a star, right. Here, just put a yield. That is a break point. It will just wait there. Wherever it finds it, it will wait. You can call this generator object and say go next or you can send some value here that will be passed to this assignment. So, that is what I am using to make this happen, right. You can look at the generator and its properties. You will find out. So, with that, I am able to do this. Now, if it is synchronous code, what would I have done? I wanted to actually send different accounts. I do not want just to, but because the asynchronous way of, I mean, with the callbacks, if I write 10 accounts, this is going to be like, you would not fit in a page. So, I just simplified it. But, if it is synchronous one, this is what I want to do. I want to loop through all the accounts and get the result. You know what? Same thing will work here also. I can loop through each and every account, take and sum the balance of each and every account and print that. So, it will print all of them and I can see the result. So, let me see if I can run that. As you can see, so I am running and I am getting all the values from async to example. So, this is the code I am running. You can see it is working. And in fact, if you think this is ugly, I just want to call the method there. Instead of directly returning a function, if I can return a generator that waits for the operation, then I can simply call it the same way as the sequential code. Only extra thing will be the yield. Wherever you want a wait to be there, wait for this to complete, just put yield. Actually, in JavaScript async await and C sharp async await, it looks exactly like this except instead of yield, you will be using await. Your function will be prefixed with async. And wherever you want to wait, you will use await. There is no await in PHP, not coming in PHP 7.3 well. So, instead of waiting, we can use whatever we have and that is very powerful. With generators, you can write coroutines and all. And now I have not touched promises. So, with the promises, we can execute certain things. Actually, we can improve this. Here, I am going through each and every account separately. So, first I get the balance of first account, then second, then third. Like that, I am going. Instead of running all of them in parallel and getting the results together and then combining that and then continuing, that will be faster. That will take down the time further. So, to do that, we can use promises. And that too can be written in a format like this. So, do parallel all these things. It will do that and get the result and then proceed to the next line. So, all that I am hoping to present and release the source code on PHPCon VAsia. Let us see if that happens. Thank you very much. Questions for Arul? Okay. Thank you. Thank you.