Welcome to the talk!
=<< What's up with that Title? >>=
It’s functions, pure functions! Pure functions and their values, PURE FUNCTIONS FOREVER AND FOREVER AND FOREVER A HUNDRED YEARS monad... some... things... Me and monads runnin' around and...applicative functor time... a- all day long forever... All a - a hundred days applicatives and functors! Forever a hundred times.... OVER and over monad transformers... adventures dot com... W W W dot at lambda calculus and category theory dot com w..w..w... function composition adventures... Ah- hundred years… Every minute referential... transparency dot com.... w w w a hundred times... composition dot com...
Fig 1.1 What most people thing a typical functional programming advocate looks like
=<< Obligatory Quote >>=
=<< Obligatory Quote >>=
If you wish to build a ship, do not divide the men into teams and send them to the forest to cut wood. Instead, teach them to long for the vast and endless sea.
=<< Promises >>=
>:(
>:(
>:(
>:(
>:(
>:(
>:(
>:(
>:(
>:(
Promises do not make things asynchronous!
=<< Promises >>=

What problem to promises solve?

  • Asynchronous request management
  • Error handling for computations which may fail
  • Probably more too

What are promises then?

  • A value (which may or may not be available yet) wrapped in some additional context
  • Functions to instantiate the wrapper (Promise.resolve, Promise.reject)
  • A rule for composing these wrapped values (then)
=<< Promises >>=

Terminology/syntax recap

JavaScript arrow functions

x => x + 1;
is equivalent to:
function(x) {
    return x+1;
}

Composition

A method of combining two things of the same type in to another thing that is also of the same type
=<< Promises >>=

Promise composition

The `then` method of a promise accepts a function as an argument. This function takes a value and returns a new promise. The new promise "combines" the previous two using the "composition rule" given to `then`.
Promise.resolve("Some value").then(x => Promise.resolve(x + " and another value")).then(x => Promise.resolve(x + " and another one!"));
There are three promises in the above snippet. One, two, three.
Promise.resolve("Some value").then(x => Promise.reject("It's all gone wrong")).then(x => Promise.resolve(x + " and another one!"));
The above snippet still results in a promise, but it is a "failed" one. Any subsequent `then` after the `reject` has no effect.
=<< Promises >>=

OK great but who cares?

  • We are declaring how we want promises to compose
  • The order that the promises actually resolve their values in is not important
    • In other words, we don't have to worry about timing anymore
  • We can declare a failure anywhere in the chain
=<< Promises >>=
atemporality?
declaritive?
functions?
composition?
=<< Promises >>=

Woah!

JavaScript

Promise.resolve("We start here")
    .then(x => Promise.resolve(x + " then get here"))
    .then(x => Promise.resolve(x + " and finally here!"))

Haskell

Right("We start here")
     >>= (\x -> Right (x ++ " then get here"))
     >>= (\x -> Right (x ++ " and finally here!"))
=<< Promises >>=

Woah!

JavaScript

Promise.resolve("We start here")
    .then(x => Promise.resolve(x + " then get here"))
    .then(x => Promise.reject("Oh noes!"))
    .then(x => Promise.resolve(x + " and finally here!"))

Haskell

Right("We start here")
    >>= (\x -> Right (x ++ " then get here"))
    >>= (\x -> Left ("Oh noes!"))
    >>= (\x -> Right (x ++ " and finally here!"))