US:+16362902365 & BD:+8801711573570

info@techgensolution.com

A Leading Web Development Company 

Sharing is caring!

If I were to try to describe this, how JavaScript is actually going to handle it here’s what I’d say. I’m going to set myself up a timer for a thousand milliseconds from now and then I’m going to come over here, I’m going to process some other work and I’ve got some other events handling. And oh, somebody clicked a button in AJAX. Oh, it’s been a thousand milliseconds, time to go print out this callback. That’s how I’d actually describe the way this code works if I were talking about it in an asynchronous fashion. The problem is that our brains don’t work asynchronously. We like to think that we’re multi-taskers but multitasking is just fast context switching. We’re still synchronous thinkers. That’s the way psychologically our brains work. All of the biological functions in fact that you talk about that happen, like my breathing and stuff like that, they happen at a subconscious level. They’re not consciously happening asynchronously. When we talk about asynchronous flow control patterns in our code, what we’re really talking about is how do I take something that is fundamentally asynchronous like a timer.

Solving Callback Problems


So I’m going to give you a couple of real quick things that callbacks try to do. These are things that people have tried to do to solve some of the issues with callbacks. For example, here we have the separate callbacks approach where I pass in a success handler and an error handler, and I expect that one or the other is called. But this is even more implicit trust because I’m trusting that you’re only going to call one and not the other. What happens if they call the success and the failure? What happens if they call the failure first and then later they call the success or vice versa? How are we supposed to handle that kind of thing? We’re trusting that they’re not going to but how would we handle that if they did? What would your program do if both callbacks got called? It would probably break. Okay. So this doesn’t really solve inversion of control, it just makes it worst. Then we have, this is commonly called node style but I think that’s a really bad name. We should called it error-first style code which is that we pass in a single callback but we get an error function. This error parameter is the first parameter there on line nine. The error parameter will be filled with something truthy if there was an error condition and it will be empty if there wasn’t. We find ourselves inside of our functions and this happens a lot in node. We do this if else all over the place. Now we don’t have two separate functions but we still have one function, and let me ask you the question. What happens if they pass back both an error object and a success value, how would your code react? You’d probably completely ignore the success value because you’d be checking only for the error object. Again, we really haven’t done anything to actually solve the implicit trust issues that have been created by callbacks as a continuation style. Okay, here’s my little running example that I’m going to give you calculating the meaning of life and here I’m doing so with nested callbacks, The first callback is I’m calling on line five. This getData function by the way, he just waits a thousand seconds and then gives you your data right back. I’m saying a thousand milliseconds from now I want you to pass me 10 back. I’ll add 10 to one and my x will now be 11. Then a thousand milliseconds from now I want you to hand me 30 back and I’ll add 30 plus one so I’ll have 31. A thousands milliseconds from now I want you to hand me back the string that says meaning of life is equal to the addition of 11 and 31. And you hand me back the answer and I print that out which is the meaning of life is 42… It’s these asynchronous steps that I’m doing that might have been AJAX calls or click handlers or any other form of stuff. And it should be evident in this code that if we did not trust that getData function we would have had a massive inversion of control trust issue. There’s also lots of problems that we have no error handling here. What happens if halfway through it fails to send us back the value where the program just hangs, you don’t have any way of doing it. So you have to construct all kinds of elaborate solutions to these problems that callbacks introduce.

 

 

Sharing is caring!

Grab the latest deals on your inbox

Your email is safe ❤️ . I hate SPAM same as you!

Techgen IT Solution provides its clients with a variety of offshore business solutions like system design, .NET technology stack including C#, ASP.NET, MVC, Web API, SQL, Entity Framework, JavaScript and related technologies, namely AngularJS, Knockout, NodeJS etc

USA OFFICE

BD OFFICE 

© 2019 Techgen IT Solution. All Rights Reserved.

shares