From b8d5028816fde0fb1727cd125537bfa49856b65a Mon Sep 17 00:00:00 2001 From: Nicholas Zuber Date: Wed, 21 Dec 2016 13:10:16 -0500 Subject: [PATCH 1/2] continuations --- readme.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/readme.md b/readme.md index a56ec49..421feef 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,7 @@ __Table of Contents__ * [Currying](#currying) * [Auto Currying](#auto-currying) * [Function Composition](#function-composition) +* [Continuation](#continuation) * [Purity](#purity) * [Side effects](#side-effects) * [Idempotent](#idempotent) @@ -175,6 +176,37 @@ const floorAndToString = compose((val) => val.toString(), Math.floor) // Usage floorAndToString(121.212121) // '121' ``` +## Continuation + +At any given point in a program, the collection of instructions that still need to be processed in order for the program to complete is known as a continuation. + +```js +const printAsString = (num) => console.log(`Given ${num}`) + +const addOneAndContinue = (num, cc) => { + const result = num + 1 + cc(result) +} + +addOneAndContinue(2, printAsString) // 'Given 3' +``` + +Continuations are often seen in asynchronous programming when the program needs to wait to receive data before it can continue. The response is often passed off to the rest of the program, which is the continuation, once it's been received. + +```js +const continueProgramWith = (data) => { + // Continues program with data +} + +readFileAsync('path/to/file', (err, response) => { + if (err) { + // handle error + return + } + continueProgramWith(response) +}) +``` + ## Purity A function is pure if the return value is only determined by its From e8ce4d0c173e869c21968b1d5dad1d97b73d9da1 Mon Sep 17 00:00:00 2001 From: Nicholas Zuber Date: Fri, 23 Dec 2016 03:29:22 -0500 Subject: [PATCH 2/2] changed wording --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 421feef..5134cd8 100644 --- a/readme.md +++ b/readme.md @@ -178,7 +178,7 @@ floorAndToString(121.212121) // '121' ## Continuation -At any given point in a program, the collection of instructions that still need to be processed in order for the program to complete is known as a continuation. +At any given point in a program, the part of the code that's yet to be executed is known as a continuation. ```js const printAsString = (num) => console.log(`Given ${num}`)