Current section: Data fetching 29 exercises
Problem

Utility

Loading exercise

Transcript

00:00 Great, it's time for us to make our utility so that we can have this simple line of code for any time we need to consume a promise. And then, of course, the pending state is handled in the parent and the suspense boundary and the error is handled with the error boundary. So beautiful. And this use hook is going to make it possible.

00:19 A utility that we're going to give to you is this use promise, which is basically just a promise that also has a couple of properties for us tracking information about the promise. So like I said, this is a bit hacky and it's implementation detail stuff, but I think that

00:35 it will give you the right mental model for working with the use hook and suspense and error boundaries. And so that's why we're doing it. So we'll have our status and value and reason tacked onto that. And then to get your use function started, this is going to be a generic function that takes a value, but that's going to just be inferred by whatever the given promise resolves

00:55 to. So you don't have to use the generic or anything like that. You notice we're not using it there. So it's all inference and it's beautiful. Ultimately, it returns the value, which is a little bit magic. And then we are going to take that promise and assign it to the type of use promise with that value.

01:13 That way we can tack on all of these properties so we can keep track of stuff. So lots of this is just going to refactor stuff that we've already got into a use function. It's going to be awesome. Let's get started.