WEBVTT 00:06.230 --> 00:12.890 In this video you going to learn rest promises in year six race promises are another great feature in 00:12.890 --> 00:15.830 E6 which will make you coding a lot easier. 00:16.400 --> 00:24.380 So let's start with an example where we've got a function print all which takes a couple promises and 00:25.260 --> 00:35.400 brings them in the console like these say if you try the function with one and two you know it's going 00:35.400 --> 00:37.750 to bring them like that. 00:37.800 --> 00:46.990 Now what if you don't know how many arguments the use a or b function will take. 00:48.560 --> 00:56.540 At some point and you still want to print all of those arguments in the console so you might think about 00:57.260 --> 01:03.990 the arguments variable which must be familiar with it. 01:03.990 --> 01:05.910 Here's five. 01:06.190 --> 01:06.650 All right. 01:07.220 --> 01:13.790 So inside this variable you can find all your arguments and just a quick reminder. 01:13.850 --> 01:15.590 This is not an array. 01:15.920 --> 01:16.280 All right. 01:16.790 --> 01:22.110 So now if you try to loop 01:26.240 --> 01:30.020 over items inside the arguments 01:38.900 --> 01:39.380 like this 01:46.070 --> 01:46.620 and then 01:49.590 --> 01:53.010 log the items like those each item index I 01:56.590 --> 02:02.740 then you can manage to get the items printed to the console like this. 02:02.740 --> 02:08.030 So you might not need all those lines. 02:08.030 --> 02:14.070 You can even skip that and this will still work. 02:14.330 --> 02:15.510 Right. 02:16.100 --> 02:22.370 Now what if you would like to return an array of all the arguments that you pass to the function instead 02:22.370 --> 02:25.620 of logging those arguments like this. 02:26.330 --> 02:35.670 So in that case you will need to define an array or an empty array. 02:35.920 --> 02:41.890 And then when you're going through your loop you're gonna be adding elements to the array using the 02:42.010 --> 02:51.900 push method like this and then you will return the array at the end. 02:51.920 --> 02:55.860 So now if you log this 02:58.930 --> 03:04.840 then you know you're going to get an array showing all the arguments plus you pass to the function. 03:05.170 --> 03:10.230 Now this is an E5 in iOS 6. 03:10.570 --> 03:14.560 There is an easier way of doing this using the rest promises 03:19.290 --> 03:20.130 say well you can do 03:29.740 --> 03:39.630 you just pass a parameter there call it X for example and use the rest parameter just before the parameter. 03:39.630 --> 03:43.380 Now if you return x straightaway 03:46.600 --> 03:47.500 let's see where he gets 03:56.230 --> 04:05.160 you get an array combining all the arguments that you pass to positive function All right so you can 04:05.160 --> 04:05.910 use the rest. 04:05.910 --> 04:11.920 Operator before a parameter to make sure it receives 04:14.290 --> 04:24.590 parameters compiled in the right and this is usually used in the arrows in the last parameter. 04:24.620 --> 04:31.820 So for example you've got a first bomb is at X and you want to combine all the remaining promises inside 04:31.820 --> 04:33.670 an array like that. 04:33.770 --> 04:39.320 So this is gonna be really useful if you don't know how many promises your function is going to take. 04:40.220 --> 04:55.190 So let's say that's just log X and log y and instead of logging the return of the function we'll just 04:55.190 --> 04:56.270 call the function like that. 04:56.690 --> 05:05.240 So you can see that we've got one of the first bombs so um the remaining promises they are compiled 05:05.840 --> 05:06.880 in the right. 05:06.920 --> 05:11.210 So this is a very useful feature in iOS 6. 05:11.330 --> 05:14.170 So now here is a quick challenge for you. 05:14.240 --> 05:25.700 So you would need to create a function passenger check which will take a couple times as first name 05:27.620 --> 05:41.890 and last name plus optional promises which will refer to the items or the belongings of the passenger. 05:42.520 --> 05:51.580 So the function will need to log something like this to the console passenger followed by the first 05:51.580 --> 05:55.510 name and last name of the passenger. 05:56.390 --> 06:09.140 And belongings and you lists all the belongings of the passenger. 06:09.160 --> 06:10.340 All right. 06:10.480 --> 06:20.990 So I suggest you post the video then find the solution using is 6 and iOS 5 and then come back once 06:20.990 --> 06:21.250 you're done. 06:23.420 --> 06:23.730 All right. 06:23.740 --> 06:26.060 So hopefully you managed to find the solution. 06:26.200 --> 06:30.970 So let's do this in E6 first which is gonna be the easiest. 06:30.970 --> 06:32.830 So let's define a function 06:36.230 --> 06:42.560 passenger check and our function is gonna take the following promises. 06:42.560 --> 06:44.890 So first firstly then last name 06:48.840 --> 06:58.320 then the belongings of the passenger so let's call them items. 06:59.410 --> 07:05.200 So we want these all the belongings to be combined or compiled into an array. 07:05.230 --> 07:08.660 So that's why we're going to use the rest operator. 07:08.920 --> 07:12.730 So now our parameter items is a risk parameter. 07:12.730 --> 07:21.670 So now our function is going to be as following so we want to log passenger followed by first name last 07:21.670 --> 07:22.200 name. 07:22.520 --> 07:22.760 So 07:30.410 --> 07:31.030 let's use 07:36.370 --> 07:37.790 template rituals. 07:37.840 --> 07:38.830 So passenger 07:42.220 --> 07:44.820 Ben first name 07:47.880 --> 07:48.710 and last name 07:55.640 --> 07:56.000 so 07:59.460 --> 08:01.320 list is just this one now. 08:01.770 --> 08:06.780 So p check all Mark 08:10.670 --> 08:11.210 Zuckerberg 08:17.110 --> 08:24.650 and we can see that we've got the right console not log off the first name and the last name which we 08:24.650 --> 08:26.300 passed to the function. 08:26.450 --> 08:31.860 Now we want to pass something else to the function the belongings of the passenger. 08:31.940 --> 08:41.230 So let's say that's he's got a bag and a suitcase. 08:41.680 --> 08:41.970 All right. 08:41.970 --> 08:51.880 So now what we need to do we're gonna need to check first if he's got any belongings if he's got some 08:51.970 --> 08:54.120 then we're gonna bring them like that. 08:54.700 --> 08:57.000 Otherwise we're gonna print none. 08:57.160 --> 09:00.030 So you're going to define a new variable. 09:00.970 --> 09:02.370 So let's call it 09:05.050 --> 09:16.120 belongings and we're going to check if the length of our array items is greater than zero. 09:16.150 --> 09:18.370 If that's the case this means that his belongings 09:22.310 --> 09:35.010 say if that's the case we're going to return items otherwise we are going to return. 09:35.110 --> 09:36.900 Not like this. 09:36.990 --> 09:38.240 All right. 09:38.700 --> 09:47.190 And then we're going to console that look we can just use the same console that look we used before 09:48.100 --> 09:55.260 and remember that you can have a line written so belongings 10:01.760 --> 10:05.340 and you can see that this is not working right. 10:05.360 --> 10:09.830 And we have called the right prints of the belongings. 10:09.830 --> 10:14.750 So if you try without any belongings you get no 10:18.020 --> 10:22.610 all right. 10:22.780 --> 10:27.690 So now let's move to iOS 6 and see how this works. 10:27.690 --> 10:28.350 Oh yes 5 10:31.830 --> 10:32.860 so it is cool. 10:33.040 --> 10:39.280 Function b check 2. 10:39.610 --> 10:43.240 This time we'll just give it a first name and last name 10:46.800 --> 10:51.570 and then we're gonna decide how we're going to process the remaining elements. 10:51.570 --> 10:58.330 So first let's log the first name and last name. 11:00.380 --> 11:01.190 So passenger 11:04.190 --> 11:12.220 plus first name plus a space plus the last thing 11:17.200 --> 11:21.310 and then we're gonna have a line return which we'll use later on 11:30.610 --> 11:31.460 so let's try this. 11:31.480 --> 11:34.680 Function wave 11:38.150 --> 11:38.660 gates 11:42.130 --> 11:44.410 so we get the right result there. 11:44.440 --> 11:49.690 So now we want to print the belongings of the passenger as well. 11:49.840 --> 11:59.780 So first we're not gonna be using the rest parameters because we are in iOS 5. 11:59.780 --> 12:04.640 So you gonna use the arguments variable like we did before. 12:04.640 --> 12:11.400 So if you log the arguments variable they just see what we call inside. 12:11.410 --> 12:18.860 So here are our arguments and now let's say that we've got the belongings. 12:18.970 --> 12:23.700 So let's say that he's got a back back. 12:23.760 --> 12:28.470 All right so here is our third argument. 12:28.550 --> 12:32.240 So now we need to be able to process these arguments so 12:35.860 --> 12:45.810 the easiest way would be to get the remaining arguments as an array and then look them straight away 12:47.380 --> 12:57.070 otherwise you can loop through the arguments variable and just skip the first two and then brings each 12:57.070 --> 12:58.010 one separately. 12:58.090 --> 12:59.300 And it's gonna be a lot of good. 13:00.310 --> 13:08.600 So the easiest here is going to be to convert the remaining arguments to an array and brings it. 13:09.510 --> 13:12.150 So what we gonna do we gonna create a variable. 13:12.900 --> 13:15.450 So let's call it z 13:20.290 --> 13:21.790 and insight Z. 13:21.860 --> 13:27.460 We're going to store the arguments from the argument. 13:27.460 --> 13:31.990 Number three we're going to carry on till the end. 13:31.990 --> 13:39.070 Now if we if you remember we used the method slice with arrays. 13:39.150 --> 13:45.560 Now the arguments variable is not in the array although you can loop over it and you can check its length 13:45.570 --> 13:46.440 for example. 13:46.440 --> 13:53.550 It's still not an array and you can't apply these lines method to arguments but there a trick we can't 13:53.580 --> 13:57.180 use these slice method of the array 14:01.350 --> 14:03.090 class like this. 14:03.090 --> 14:14.680 And then we're gonna use the call method to specify that this is gonna be our arguments variable. 14:14.730 --> 14:15.260 All right. 14:15.840 --> 14:17.790 So if you do this and you log 14:20.830 --> 14:26.080 that let's see what we get we go. 14:26.640 --> 14:33.440 So you starting from the elements of index 2 which is that 1. 14:34.230 --> 14:41.520 And if you don't specify where you ending then you carry on till the end. 14:42.380 --> 14:51.060 All right so you know a case we of course only one parameter or one element back back. 14:51.100 --> 14:54.900 That's why we've got only one inside the array. 14:54.910 --> 14:56.590 If you aren't something else that 15:01.350 --> 15:04.830 then you're gonna get to know right now. 15:10.040 --> 15:22.200 Nets are these that so belongings and then we are doing this it quite perfect. 15:22.470 --> 15:24.790 Now what if we don't have any belongings. 15:24.810 --> 15:25.440 So what's happens 15:28.230 --> 15:29.110 so it's gonna be empty. 15:29.120 --> 15:31.690 And we want to bring it non instead. 15:31.970 --> 15:38.120 Say what are you gonna do you're going to check the length of our array 15:41.070 --> 15:49.710 and we can define a new variable says called his belongings so we're checking the length of said if 15:49.710 --> 15:57.510 it's greater than 0 then it's gonna be Z. 15:57.710 --> 16:07.580 Otherwise it's going to be not you know instead of looking Z we're going to log the variable belongings. 16:07.630 --> 16:07.870 All right. 16:07.870 --> 16:21.440 So let's save and try with belongings and then without belongings and this time we get more perfect. 16:21.690 --> 16:28.570 So this was a good example showing how the rest promises can make your coding a lot easier in Essex. 16:28.650 --> 16:35.190 So you didn't have to use these lines method or the call method you just had to use the rest parameter 16:36.500 --> 16:40.730 and write some code straightaway. 16:40.730 --> 16:40.930 Right. 16:40.940 --> 16:45.230 So in the next video you're going to learn the spread operator.