Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course

The "Creating a Weather Module" Lesson is part of the full, Hardcore Functional Programming in JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how to use the Task monad when creating a weather module, and how to use the monad in different places within the same application.

Preview
Close

Transcript from the "Creating a Weather Module" Lesson

[00:00:00]
>> All right, what else are we going to do here. We got like 15 more minutes. Let's just throw some stuff at it. So we're gonna do here is, so the question I get a lot is, well, like I see this all dumped into the same file. Why don't I make a little open weather module, right.

[00:00:19]
Now this is a private function. This is a private function. So I can go ahead and I can do this. I'm gonna make a new file called open_weather.js. And I'm just gonna paste this stuff in. And now, look, I got private functions, I'm only gonna export Open weather.

[00:00:48]
And I'm gonna bring in my API key, my task and perhaps now, API key is starting to become a thing, right? We're just like import it directly. We want to pass it in. So, let's get that out of there [LAUGH] And, get this out of here. We're just gonna do import the Open weather from .open_weather, great.

[00:01:20]
Cool, so now we're in and like, we've still got our like, happy Puroland up here, but we're actually doing anything anymore, right? We're just gonna call open weather fetch. And it looks like we do have to pass in the API key as well, right? So there's many ways to handle this.

[00:01:42]
My preferred way, as we'll see tomorrow, is that in the functional architecture class would be to use the reader monad to hide this and only pull it out of thin air when you need it. We're just gonna pass it in right now. And it's really not that different.

[00:02:01]
No big deal. So we get an API key here and we'll just pass that in. And hey, look, we can do that thing where we turn it into both arguments at once. So instead of taking a zip and an API key separately, I know that's always equivalent to taking both.

[00:02:25]
So I'm just passing both in his array, or we could pass an object with both. That's pretty fancy. Let's do that. Cool, let's go back. We packaged it up. We knew that was the same packaged up. Everybody's happy. Let's give it a shot. It's there. Why are you so mad at me?

[00:02:46]
Our zip is undefined. And we have course [LAUGH] awesome. Because here comes the code yeah, let's change it back to zip. I don't know why I got fancy with zip code. Boom, all right. So everybody's still happy but we have to call .Jason on it. Okay? So why don't we do that right off the bat.

[00:03:03]
Let's do that down here. So we'll just do a then actually, this is fetch it and do. How about a 2Jason here, right? And we have to map that, right. Because we have a task, we're gonna crack open the task and call 2 Jason. So we get x, x out, Jason, great.

[00:03:28]
So nice, [LAUGH] not happy, we don't have a map defined. Bring that one in, I know I'm kind of flying through this you all. I know we have limited time, promise pending that's not what we wanted. We can coerce this into a task but let's forget it, I'm just gonna do it right here [LAUGH] for the sake of time.

[00:03:54]
Let's just do that there, get this out of here. But we could have said, okay, we have a fetch call that returns a promise though, like we have a task that's capturing a promise. Then we have another Jason call, it's a promise that we want to make a task that resolves.

[00:04:10]
When the promise resolves, return both promise functions into task functions and will totally capture it. I don't feel like going through that. I feel like that's boring. So let's just keep hearing on.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now