تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Introducing executeAsync Promises0:00

We just looked at HelloWorld.executeMany as one way to execute the function. We've been doing HelloWorld.execute. Now I want to look at HelloWorld.executeAsync. And you may be thinking, php is not an async language, and you're correct, it's not. And so that's what makes this one so weird. This returns a promise, but it's not totally like a promise you would get in JavaScript world. It is a little bit different, and it has a couple of weird gotchas. For us to fully understand how this works, we need to talk about some dependencies really quick.

Dependencies and Guzzle Promises0:39

For us to fully understand how this works, we need to talk about some dependencies really quick. So, Sidecar relies on the AWS SDK. The AWS SDK relies on Guzzle underneath it. Laravel also relies on Guzzle. So you should be pretty familiar with Guzzle. The thing that is returned from executeAsync is a Guzzle promise. What's weird though, and what's unlike JavaScript, is that this doesn't actually start the request. This doesn't send the invocation to AWS. So while you're doing some work here, this is not running.

Demonstrating Non-Execution1:39

wait for it? Doesn't that make it synchronous? Well, to understand what's actually happening, we need to look into Guzzle just a little bit. Let's kill this, and let's kill this. Before we dive fully into Guzzle, I just want to show you that this is not being executed. So we'll return hello directly to the browser without waiting on the promise. We'll go to serverless.whip, and you see it comes back in 164 milliseconds. Again, now we're looking at about 50 milliseconds. So we know that our 250 millisecond lambda function isn't running, because the response

Again, now we're looking at about 50 milliseconds. So we know that our 250 millisecond lambda function isn't running, because the response is coming back way quicker than that, and it's not running outside of the request-response cycle. It's just not running at all. If we were to wait on it, let's put my name back in here, and the way that you wait on it is you wait for the settledResult. The settledResult is the promise that has settled. So now if we return promiseBody, and this is technically, this is better called the result here.

How Guzzle Queue Works3:25

There is a way to fire and forget, and that's executing it as an event, which we'll cover in the next video. So when you execute it async, I told you that you get back a Promise, and that it's a GuzzlePromise. What Guzzle does behind the scenes is it puts all of these async calls into its own queue, and it's a global queue. It's a static function. So if we look at utils, and it's in the GuzzlePromise namespace, we can just pull queue. This is the global queue that Guzzle puts all of the pending requests in. So if we, let's just dump this here, and then we'll dd it down below.

This is the global queue that guzzle puts all of the pending requests in. So if we, let's just dump this here, and then we'll dd it down below. If we refresh, you see it's a guzzle promise task queue. In the beginning, it's empty. After that, it has one thing in the queue. So remember I said it doesn't execute until you wait for it. What it does do, however, is it puts it in the guzzle queue. Let's go back to web, and let's just add a couple more. So we'll say promise one, two, and three. Now you see that our queue, rather guzzle's queue that it's maintaining for us, is full.

Parallel Different Lambda Calls7:11

first one is going to kick off the whole queue, and the rest of them are going to be done. So why am I showing you this if it's already handled by executeMany, and this is just a worse and more painful way of doing it? executeMany executes the same function over and over and over, right? What if, as a part of some process, you had to run a lambda to do one thing, a different lambda to do something else, and maybe a third, fourth, or fifth lambda function to do other things, and you wanted to run them in parallel? Well, in that case, you could do it like this. You could have three different functions. We'll run browser function twice, and we'll run it with different data each time.

You could have three different functions. We'll run browser function twice, and we'll run it with different data each time. In fact, let's run four functions, and we'll run it with two sets of data. So we'll run Jeffrey Aaron, Jeffrey Aaron. Now we have four promises that haven't been executed yet, and importantly, they're different lambda functions, right? So now we can't use executeMany because they're different functions. So what we'll do is we'll do promises equals this array. Then at the end, we will array_map over the promises, passing the promise through, and we'll call promise.

Then at the end, we will array_map over the promises, passing the promise through, and we'll call promise. Remember this is going to be a pending result, so we can call promise settled. Now Guzzle, on our behalf, will send all four of these simultaneously. Then we will make sure that they're all done, and now down here, we can do whatever we want with the results because now all four promises have settled. All right, let's review where we would use executeAsync just so it's totally clear. executeAsync is great when you need to execute more than one type of function because if you were just doing these helloWorld functions, it would be easier to just do executeMany. If you were just executing one, it would be easier to just do execute because then you

When to Use Each Method9:57

Well that's where our last one comes in, and that's execute as event, and we'll take a look at that one in the next video.

Promise-like Behavior

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟