Sanctuary JS

Sanctuary

Found this new (to me) FP library called Sanctuary.

Total Functions

It has total functions; unlike Ramda/Lodash it validates types at runtime; no need to check for the correct data type before using functions.

Information Preservation

It makes intelligent decisions around results being ADT’s. You get back a Maybe for things in Lodash that would report a String or undefined. A lot more clear and something you’d end up probably wrapping yourself anyway.

Currying: Only 1 Arg Per Function

Strong opinions that currying should be 1 argument per function. I’m on the fence about this, mainly because currying is a huge leap for some JavaScript programmers & having “the old way” to call functions normally like add(1, 2, 3) vs add(1)(2)(3) helps them out a lot to learn.

No this

Disallows this. It’s the worst thing in JavaScript & more divisive than politics.

Throws For Wrong Type

This kind of makes me rage is what they call invariants. Meaning, if you pass in the wrong type, it throws. Now, during unit testing + manual testing at runtime, this can help suss out bugs even if you’re using typings via Flow/TypeScript, etc. However, throwing Errors is a side effect, and it blows my mind a group smart enough to create Sanctuary would do this. Good news, tho, you can turn it off, heh!

Learn More

Read more about Sanctuary.

Node.js Crash Course

Introduction

I’ve been doing Node full-time at work and noticed a lot of other people lacking a centralized resource to get up and running quickly. There are a lot of wonderful resources out there for Node, a Google search away, but hopefully this document should get you coding quickly as well as able to communicate effectively with other Node developers.

I’ve tried to write this list in order of most important things you need to know. Feel free to skip around.
Continue reading “Node.js Crash Course”

Easier Error Handling Using Async/Await

Introduction

At work, someone asked if there were any better ways to handle errors when using the async/await syntax in JavaScript. They didn’t like their beautiful, short, and readable lines of code suddenly wrapped with try/catches. I’ve also been frustrated with a variety of the enthusiasm online the past couple years around async/await only to be shown code examples that completely ignore error handling.

Below is an easier way to handle errors using async/await by returning what’s known in Functional Programming as an Either. Mine isn’t as formal as the FP community’s “left right”. It’s just simple JavaScript Object that follows the Node callback naming convention somewhat.

tl;dr; First option is to create Promises that only call success with an Either, and Promise.resolve in the catch with an Either, or second option is to use a simple wrapper function.
Continue reading “Easier Error Handling Using Async/Await”

Asynchronous Programming

Introduction

JavaScript is an asynchronous programming language in Node and in the browser. In many languages such as Java, C#, Python, etc. they block the thread for I/O. What this means is when you make an HTTP/ajax call or read a text file for example, the runtime will pause on that line of code until it is successful or failure.

JavaScript does the opposite. Using callbacks or Promises, you basically leave a phone number to call when those operations are done, while the rest of the synchronous code keeps going. In this article we’ll talk about why, give examples from JavaScript and compare against a blocking language, and show you some tips to help.

Continue reading “Asynchronous Programming”