- Now since we recently talked about generics
I kind of want to expand on that a little further by talking
about how do you deal with asynchronous functions,
namely when you're working with promises.
'Cause this is another use case where generics come
in really handy.
For example, let's say I wanted to wait
for 1,000 milliseconds
and then I wanted to do something with my value.
And let's just come in here.
I know that my value here is going to
be a string that's being returned.
So I could do something like value.length
and log that out to see how long my string is.
You're noticing when I do that,
I actually get an error and that's
because it doesn't know what the type of value is.
It has a type of unknown
for the value 'cause it's like, what is this value?
I don't know.
The reason for that is our promise expects a generic type to
determine what the resolve function here is going to be,
'cause right now it doesn't know what that type
is going to be.
Even though I've called it with the value of high here,
it still doesn't know what that is going to be resolving to.
So when you create a promise, you can actually
pass in here a generic type, and in my case
I know that this is going to be a string.
By doing that, I've cleared
up all of my different errors for this.
So when I'm working
with a function that's dealing with a promise,
I need to make sure I specify the generic for that promise.
Now I can go a step further
by actually declaring the return type for my function.
'Cause if you want to return from a asynchronous function,
you're going to be using promises for that,
so you need to make sure the return type here is a promise
with that specific generic type added onto it.
Now this is automatically inferred for us
so we don't have to add this ourselves
but if you're dealing with that asynchronously
that is one thing to be aware of.
And another really important thing to be aware
of is if you have a function that's actually specified
as an async function and you're doing some asynchronous code
inside of here, for example, let's just say we
have a really simple fetch request going on
and then we just return the value from here.
This actually also is going to be returning a promise
from it as well.
So in our case, we need to make sure we specify
what that promise is going to be.
So we could say like a string, or in our case
we could just say any, it doesn't really matter.
Now if we remove this and let it just infer,
fetch already has the promise type enabled.
You can see it returns a promise response.
So down here,
our value is going to be that specific response object.
So I can do like JSON, for example, inside of here
and that's going to work just fine.
The important thing to note though,
is if you have an asynchronous function like this
the return type always needs to be a promise.
So when you're consuming that
it's always going to be a promise return type.
So if you need to manually type it
you must make sure you wrap it inside of a promise
whatever's actually being returned.