Transcript from the "Introducing Asynchronicity" Lesson
>> Will Sentance: We're going to move on to now the wonderful world of asynchronicity. Asynchronicity is going to change up this whole model. I'm gonna change it up. It's going to augment this whole model. This whole model is going to continue to exist. In fact, the reason we are going to need to augment our model, is this model is going to be fundamentally untenable in how we think about doing tasks that take a long time.
>> Will Sentance: To executing the next multiply by 2 at the same time. I'm not allowed to, single threaded.
[00:01:08] I never, ever move on to the next line. If you see a function clue, I must return out of it. I must finish executing it before I hit the next line. Well, this implies a big problem. What if I multiply by two call where I call to a server, that was gonna take 300 milliseconds?
[00:01:55] That can't happen, I'll be sitting there clicking because I'm still busy waiting on the line saying go get more tweets. Because I've gotta wait til it returns back the tweets, right, before I can move on to my next line that says, color the heart pink, cuz I clicked the button.
[00:02:10] This is gonna be a profound issue. What if we want to wait some time before we can actually use or own bits of code. We want to wait until our data comes back from Twitter, before we can then display and run the functionality, display that data.
>> Will Sentance: Perhaps we need to get more data from the API, or even a timer to complete, and then we wanna run some code.
[00:02:33] We have a conundrum. A tension between wanting to delay some code running, because we're waiting for data to come back, so we wanna have the next line of code display those tweets. We wanna add the data to actually come back. But we do know to block our single thread from continuing to run code.
>> Alec: Alec.
>> Alec: APIs?
[00:03:47] Where are the features that speak to the Internet that Java should be using? Yeah, what does that mean?
>> Alec: I don't know, restful web services and.
>> Will Sentance: Michelle, where are those features that, for example, can speak to the Internet?
>> Speaker 3: Are the in the browser [CROSSTALK]?
>> Will Sentance: They're in the browser.
[00:04:50] Here is our first solution. And I'll pre tell you that it's fundamentally untenable. But here's our first solution for how to go and do a task that is going to take a long time like speaking to Twitter and getting our tweets back. And nevertheless, be able to continue running code, but know where that data is when it comes back.
[00:05:11] This first solution here is going to be untenable, but it is nevertheless going to be fairly intuitive, but completely untenable. All right. Line 1, and by the way, so far we're still seeing things that are very analogous to the first hard parts, but we need to get these foundations down to understand the harder pieces to come.
[00:05:36] All right, line 1, Abdie?
>> Abdi: Well, we're declaring the display in our variable environment.
>> Will Sentance: Yeah, very nicely put, Abdie. Displays declared, excellent. Next line of code, Alec? Do the left hand side first.
>> Abdi: We're declaring data from API.
>> Will Sentance: Yeah, and what's it gonna be assigned?
>> Abdi: The fetchAndWait function.
>> Speaker 3: Decode a function.
>> Will Sentance: Decode. The right hand side here is a command. It is unfinished work. So be really clear with yourself in no way are we assigned. Data API has zero interest in fetchAndWait. Its only interest is in whatever gets returned from fetchAndWait, which we hope will be our tweet.
[00:06:42] Okay, so fetchAndWait here is a made-up function, but let's just see what it does. So data from API is going to be the return of fetchAndWait to our Twitter URL. So we're hoping it's gonna return a nice tweet for us. That's what we're hoping. So let's start tracking our time passing.
[00:07:12] We're at about one millisecond here. Let's say this thing here takes 200 milliseconds to complete.
>> Will Sentance: 200 milliseconds later,
[00:07:58] So we better have that data back if we're gonna display it. This is our solution 1. Be clear, this is not the right solution, but this is a solution. So 200 milliseconds passes, and our request to Twitter returns out, let's just say, very nicely it returns out our data, and it is the, it is the single Tweet, and it's not even coming back as an object, of course they come back as objects.
[00:08:25] But just a Tweet high. And we're going to store high, where, Michelle?
>> Michelle: In data from API.
>> Will Sentance: In data from API. I apologize for sounding like I'm correcting all of your pronunciations. Of date to data, but I'm not. I embrace your mispronunciations. Okay. Well done all of you for corrupt, no, not corrupting.
[00:09:16] Sonny, what's our next line of code say to do?
>> Sonny: To pass the data API to display as high.
>> Will Sentance: Right. So we pass out to recalling the display function with our argument being the value of data from API, everything in Java is we're going to evaluate it immediately, data from API here is a string high', we throw it straight in.
[00:09:39] And we create using context to run it where in the memory, this is really clear here, in the memory data the parameter data is set to that argument high. And so we're going to log in our console. At about 201 milliseconds we're going to log high. Okay. And now finally, what line do we hit next?
[00:10:05] Gentleman, what's your name?
>> Ben: Ben
>> Will Sentance: Ben. Sorry, Ben.
>> Ben: We're gonna log me later.
>> Will Sentance: Me later is gonna be logged at the much later time of 202 milliseconds, me later is logged.
>> Will Sentance: Raise your hand if you're a huge fan of this solution. I'm a huge fan of this.
[00:10:34] No, I don't mind the solution. Here's why I don't mind the solution, is it is profoundly intuitive. Three goals we have, three goals we have. Be able to do tasks that take a long time, slow tasks, like getting data from Twitter. We have to be able to do that.
[00:11:19] In this solution, not a hard bit. This solution, it's effortless. I can see where my data is synchronously in order, in memory. I've got no issues when my display function is gonna run. It ain't gonna run literally in my single thread until I got my data back. I'm dealing only in the synchronous world here, in order, in order top to bottom.