1 00:00:00,180 --> 00:00:01,920 - Now since we recently talked about generics 2 00:00:01,920 --> 00:00:04,410 I kind of want to expand on that a little further by talking 3 00:00:04,410 --> 00:00:07,020 about how do you deal with asynchronous functions, 4 00:00:07,020 --> 00:00:08,970 namely when you're working with promises. 5 00:00:08,970 --> 00:00:10,830 'Cause this is another use case where generics come 6 00:00:10,830 --> 00:00:12,090 in really handy. 7 00:00:12,090 --> 00:00:13,620 For example, let's say I wanted to wait 8 00:00:13,620 --> 00:00:15,540 for 1,000 milliseconds 9 00:00:15,540 --> 00:00:18,300 and then I wanted to do something with my value. 10 00:00:18,300 --> 00:00:19,410 And let's just come in here. 11 00:00:19,410 --> 00:00:20,880 I know that my value here is going to 12 00:00:20,880 --> 00:00:22,500 be a string that's being returned. 13 00:00:22,500 --> 00:00:24,450 So I could do something like value.length 14 00:00:24,450 --> 00:00:27,243 and log that out to see how long my string is. 15 00:00:28,440 --> 00:00:29,910 You're noticing when I do that, 16 00:00:29,910 --> 00:00:31,560 I actually get an error and that's 17 00:00:31,560 --> 00:00:33,570 because it doesn't know what the type of value is. 18 00:00:33,570 --> 00:00:35,040 It has a type of unknown 19 00:00:35,040 --> 00:00:36,750 for the value 'cause it's like, what is this value? 20 00:00:36,750 --> 00:00:37,650 I don't know. 21 00:00:37,650 --> 00:00:41,100 The reason for that is our promise expects a generic type to 22 00:00:41,100 --> 00:00:44,130 determine what the resolve function here is going to be, 23 00:00:44,130 --> 00:00:46,020 'cause right now it doesn't know what that type 24 00:00:46,020 --> 00:00:46,853 is going to be. 25 00:00:46,853 --> 00:00:49,050 Even though I've called it with the value of high here, 26 00:00:49,050 --> 00:00:52,380 it still doesn't know what that is going to be resolving to. 27 00:00:52,380 --> 00:00:53,911 So when you create a promise, you can actually 28 00:00:53,911 --> 00:00:56,640 pass in here a generic type, and in my case 29 00:00:56,640 --> 00:00:58,590 I know that this is going to be a string. 30 00:00:58,590 --> 00:00:59,550 By doing that, I've cleared 31 00:00:59,550 --> 00:01:02,040 up all of my different errors for this. 32 00:01:02,040 --> 00:01:02,873 So when I'm working 33 00:01:02,873 --> 00:01:04,530 with a function that's dealing with a promise, 34 00:01:04,530 --> 00:01:07,410 I need to make sure I specify the generic for that promise. 35 00:01:07,410 --> 00:01:08,910 Now I can go a step further 36 00:01:08,910 --> 00:01:11,100 by actually declaring the return type for my function. 37 00:01:11,100 --> 00:01:13,770 'Cause if you want to return from a asynchronous function, 38 00:01:13,770 --> 00:01:15,540 you're going to be using promises for that, 39 00:01:15,540 --> 00:01:17,910 so you need to make sure the return type here is a promise 40 00:01:17,910 --> 00:01:20,790 with that specific generic type added onto it. 41 00:01:20,790 --> 00:01:22,530 Now this is automatically inferred for us 42 00:01:22,530 --> 00:01:24,030 so we don't have to add this ourselves 43 00:01:24,030 --> 00:01:25,980 but if you're dealing with that asynchronously 44 00:01:25,980 --> 00:01:27,870 that is one thing to be aware of. 45 00:01:27,870 --> 00:01:29,580 And another really important thing to be aware 46 00:01:29,580 --> 00:01:31,890 of is if you have a function that's actually specified 47 00:01:31,890 --> 00:01:34,800 as an async function and you're doing some asynchronous code 48 00:01:34,800 --> 00:01:36,660 inside of here, for example, let's just say we 49 00:01:36,660 --> 00:01:38,763 have a really simple fetch request going on 50 00:01:38,763 --> 00:01:41,340 and then we just return the value from here. 51 00:01:41,340 --> 00:01:44,460 This actually also is going to be returning a promise 52 00:01:44,460 --> 00:01:45,690 from it as well. 53 00:01:45,690 --> 00:01:47,580 So in our case, we need to make sure we specify 54 00:01:47,580 --> 00:01:48,900 what that promise is going to be. 55 00:01:48,900 --> 00:01:50,790 So we could say like a string, or in our case 56 00:01:50,790 --> 00:01:52,740 we could just say any, it doesn't really matter. 57 00:01:52,740 --> 00:01:54,620 Now if we remove this and let it just infer, 58 00:01:54,620 --> 00:01:57,150 fetch already has the promise type enabled. 59 00:01:57,150 --> 00:01:59,100 You can see it returns a promise response. 60 00:01:59,100 --> 00:01:59,933 So down here, 61 00:01:59,933 --> 00:02:02,640 our value is going to be that specific response object. 62 00:02:02,640 --> 00:02:05,190 So I can do like JSON, for example, inside of here 63 00:02:05,190 --> 00:02:06,690 and that's going to work just fine. 64 00:02:06,690 --> 00:02:07,560 The important thing to note though, 65 00:02:07,560 --> 00:02:09,870 is if you have an asynchronous function like this 66 00:02:09,870 --> 00:02:12,210 the return type always needs to be a promise. 67 00:02:12,210 --> 00:02:13,650 So when you're consuming that 68 00:02:13,650 --> 00:02:16,290 it's always going to be a promise return type. 69 00:02:16,290 --> 00:02:17,790 So if you need to manually type it 70 00:02:17,790 --> 00:02:20,010 you must make sure you wrap it inside of a promise 71 00:02:20,010 --> 00:02:21,760 whatever's actually being returned.