Our custom publisher is working out pretty well but there are one or two little fixes I want to add
in rather quickly.
So first off inside of our publisher T.S. file we create the publisher right here and then we call publish
publishing any bend over to that streaming server is an asynchronous operation.
The node that streaming library is going to make requests over policy event and then get some confirmation
that it actually was published there's probably gonna be many scenarios where we want to somehow wait
for an event to be published before doing something else.
In our code.
So in other words it would be really nice field to do and a wait and wait for the publish to be completed
successfully.
So in this video we're going to figure out how we can somehow add in this awake key word right here
on publish and have it actually do something.
All right.
So to implement this we're going to go back over to our base publisher and then inside the base holds
is your class we're gonna find a publish function in order to use the async await syntax.
All we have to do is return a promise that we're gonna create manually from this published function
and inside that promise function we're going to resolve it ourselves whenever the callback function
is invoked and rejected.
If the callback function is invoked with an error let's first just focus on setting up the promise itself
inside a publish.
I'm going to return a new promise.
So if you've never created a promise on your own.
This is how we do it.
We're gonna pass in a function to the promise.
And this is going to be called with two arguments result and reject.
So if you want the promise to resolve itself and essentially be completed successfully we will call
this resolve function.
Otherwise if something goes wrong we'll call reject will then take our client published code right there.
I'm going to cut all that and move it into the promise
then inside the callback the callback is what gets invoked after the Nazi streaming library has actually
published this event successfully.
The first argument to this callback function is an air object.
This will be no if no error occurred or it will be some error if something went wrong.
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
going to reject this promise with the air that just occurred otherwise.
If we get past that rather than doing a plane console log we will resolve the promise.
Now if you hover over publish you'll notice that the type annotation on here is promise that's going
to resolve with a value of a known or a type of unknown.
We aren't really resolving with anything so we're going to put on a custom annotation here of promise
and say this is going to resolve with nothing at all.
So now it has a more appropriate promise type OK.
So now we can save this we'll go back over to our publisher t s file.
So this is where we created our publisher.
We should now be able to mark the enclosing function as async and then put a weight on a public statement
and not get any air from it.
So now we're going to wait for the publisher to be complete successfully before executing any other
code.
If we care about capturing an error that might occur for example maybe we're not able to connect to
the Nat streaming server.
We could always wrap this inside of a try catch statement catch the error and then maybe do a console
dot error with it like so let's say this go back over to our terminal and do a quick test going to make
sure my listeners are running and the publisher is running.
I'll then restart the publisher
and not quite starting up just yet.
Always because I removed the console log that's why if I cleared the listener.
Yeah the event does get emitted.
I just removed the console log where we were saying oh yeah we successfully publish the thing.
I think it is nice to leave in a console log whenever we successfully publish an event just so we can
kind of verify look at the logs and understand when these things are being published.
So I mean that was kind of a case in point right there.
I thought my code was not running successfully so I'm going to go back over the base publisher and right
after we do the check for an error right above the resolve I will do a console log right there and say
event published two subject and then I'll print out this dot subject let's save that put back over that's
a little bit better.
So now we can see you then publish to a subject ticket colon created.
So this looks good.
Definitely a good improvement.
Let's take a quick pause right here.
I want to make one more improvement in the next video.