Alexander furtively enters the room. He takes the seat beside me and fiddles with a pen. “Ryan” he starts, and I can tell he’s really uncomfortable now. “Ryan, I think I just made a factory in JavaScript”. He cringes when he says JavaScript.
Alexander is uncomfortable because, right now, functional is cool and by writing a factory, he’s done something very uncool, he’s brought an object oriented idiom into a place it doesn’t belong. Factories are prevalent in Java, that similarly named language that came out at roughly the same time. Factories are one way to solve the problem: we want something made but we don’t want to concern ourselves with how it’s made. A parallel to the society we live in.
Since the industrial revolution that was Java, we have come to depend more and more on factories. But there is a cleaner, more functional way, a callback to a more civilised time.
So I said to Alexander, there is a way you can still hide the intricacies of manufacturing and not require a factory. Instead take the options we would have passed to the factory and pass them with a callback –a continuation– to the creation procedure. We regain control when the continuation is called. This approach: continuation-passing style, is cleaner as there are fewer moving parts and, when made asynchronous is oh so cool.