1 00:00:01,670 --> 00:00:05,870 Our custom publisher is working out pretty well but there are one or two little fixes I want to add 2 00:00:05,870 --> 00:00:07,130 in rather quickly. 3 00:00:07,130 --> 00:00:12,080 So first off inside of our publisher T.S. file we create the publisher right here and then we call publish 4 00:00:12,470 --> 00:00:16,940 publishing any bend over to that streaming server is an asynchronous operation. 5 00:00:17,030 --> 00:00:21,650 The node that streaming library is going to make requests over policy event and then get some confirmation 6 00:00:21,650 --> 00:00:26,210 that it actually was published there's probably gonna be many scenarios where we want to somehow wait 7 00:00:26,240 --> 00:00:28,990 for an event to be published before doing something else. 8 00:00:29,000 --> 00:00:30,110 In our code. 9 00:00:30,110 --> 00:00:36,200 So in other words it would be really nice field to do and a wait and wait for the publish to be completed 10 00:00:36,200 --> 00:00:37,540 successfully. 11 00:00:37,580 --> 00:00:41,660 So in this video we're going to figure out how we can somehow add in this awake key word right here 12 00:00:41,870 --> 00:00:45,250 on publish and have it actually do something. 13 00:00:45,410 --> 00:00:45,640 All right. 14 00:00:45,650 --> 00:00:51,330 So to implement this we're going to go back over to our base publisher and then inside the base holds 15 00:00:51,340 --> 00:00:56,270 is your class we're gonna find a publish function in order to use the async await syntax. 16 00:00:56,270 --> 00:01:00,770 All we have to do is return a promise that we're gonna create manually from this published function 17 00:01:01,420 --> 00:01:05,780 and inside that promise function we're going to resolve it ourselves whenever the callback function 18 00:01:05,810 --> 00:01:07,450 is invoked and rejected. 19 00:01:07,460 --> 00:01:12,070 If the callback function is invoked with an error let's first just focus on setting up the promise itself 20 00:01:12,960 --> 00:01:14,720 inside a publish. 21 00:01:14,920 --> 00:01:17,610 I'm going to return a new promise. 22 00:01:17,650 --> 00:01:19,940 So if you've never created a promise on your own. 23 00:01:19,940 --> 00:01:21,650 This is how we do it. 24 00:01:21,850 --> 00:01:24,040 We're gonna pass in a function to the promise. 25 00:01:24,250 --> 00:01:27,220 And this is going to be called with two arguments result and reject. 26 00:01:27,730 --> 00:01:32,680 So if you want the promise to resolve itself and essentially be completed successfully we will call 27 00:01:32,710 --> 00:01:34,120 this resolve function. 28 00:01:34,210 --> 00:01:40,180 Otherwise if something goes wrong we'll call reject will then take our client published code right there. 29 00:01:40,300 --> 00:01:43,160 I'm going to cut all that and move it into the promise 30 00:01:46,010 --> 00:01:51,740 then inside the callback the callback is what gets invoked after the Nazi streaming library has actually 31 00:01:51,740 --> 00:01:53,720 published this event successfully. 32 00:01:53,720 --> 00:01:57,290 The first argument to this callback function is an air object. 33 00:01:57,290 --> 00:02:02,090 This will be no if no error occurred or it will be some error if something went wrong. 34 00:02:02,270 --> 00:02:08,240 So we're gonna take a look at that air we'll say if there isn't air then let's return early and we're 35 00:02:08,240 --> 00:02:13,200 going to reject this promise with the air that just occurred otherwise. 36 00:02:13,210 --> 00:02:21,770 If we get past that rather than doing a plane console log we will resolve the promise. 37 00:02:21,790 --> 00:02:26,860 Now if you hover over publish you'll notice that the type annotation on here is promise that's going 38 00:02:26,860 --> 00:02:30,410 to resolve with a value of a known or a type of unknown. 39 00:02:30,520 --> 00:02:35,140 We aren't really resolving with anything so we're going to put on a custom annotation here of promise 40 00:02:35,650 --> 00:02:38,170 and say this is going to resolve with nothing at all. 41 00:02:38,170 --> 00:02:44,280 So now it has a more appropriate promise type OK. 42 00:02:44,360 --> 00:02:48,560 So now we can save this we'll go back over to our publisher t s file. 43 00:02:48,560 --> 00:02:50,260 So this is where we created our publisher. 44 00:02:50,350 --> 00:02:55,640 We should now be able to mark the enclosing function as async and then put a weight on a public statement 45 00:02:55,940 --> 00:02:57,640 and not get any air from it. 46 00:02:58,120 --> 00:03:02,150 So now we're going to wait for the publisher to be complete successfully before executing any other 47 00:03:02,150 --> 00:03:02,960 code. 48 00:03:02,960 --> 00:03:06,950 If we care about capturing an error that might occur for example maybe we're not able to connect to 49 00:03:06,950 --> 00:03:08,510 the Nat streaming server. 50 00:03:08,510 --> 00:03:15,130 We could always wrap this inside of a try catch statement catch the error and then maybe do a console 51 00:03:15,130 --> 00:03:22,860 dot error with it like so let's say this go back over to our terminal and do a quick test going to make 52 00:03:22,860 --> 00:03:26,090 sure my listeners are running and the publisher is running. 53 00:03:26,160 --> 00:03:27,300 I'll then restart the publisher 54 00:03:29,780 --> 00:03:32,330 and not quite starting up just yet. 55 00:03:33,670 --> 00:03:38,270 Always because I removed the console log that's why if I cleared the listener. 56 00:03:38,290 --> 00:03:39,880 Yeah the event does get emitted. 57 00:03:39,880 --> 00:03:44,320 I just removed the console log where we were saying oh yeah we successfully publish the thing. 58 00:03:44,320 --> 00:03:48,550 I think it is nice to leave in a console log whenever we successfully publish an event just so we can 59 00:03:48,550 --> 00:03:52,250 kind of verify look at the logs and understand when these things are being published. 60 00:03:52,300 --> 00:03:54,690 So I mean that was kind of a case in point right there. 61 00:03:54,700 --> 00:04:00,140 I thought my code was not running successfully so I'm going to go back over the base publisher and right 62 00:04:00,200 --> 00:04:06,260 after we do the check for an error right above the resolve I will do a console log right there and say 63 00:04:06,320 --> 00:04:16,510 event published two subject and then I'll print out this dot subject let's save that put back over that's 64 00:04:16,510 --> 00:04:17,110 a little bit better. 65 00:04:17,110 --> 00:04:21,240 So now we can see you then publish to a subject ticket colon created. 66 00:04:21,400 --> 00:04:22,150 So this looks good. 67 00:04:22,150 --> 00:04:24,220 Definitely a good improvement. 68 00:04:24,220 --> 00:04:25,360 Let's take a quick pause right here. 69 00:04:25,360 --> 00:04:27,490 I want to make one more improvement in the next video.