Skip to content

Commit 2164426

Browse files
authored
Merge pull request #130 from nickzuber/master
Continuations
2 parents 82fb37e + e8ce4d0 commit 2164426

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

readme.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ __Table of Contents__
2121
* [Currying](#currying)
2222
* [Auto Currying](#auto-currying)
2323
* [Function Composition](#function-composition)
24+
* [Continuation](#continuation)
2425
* [Purity](#purity)
2526
* [Side effects](#side-effects)
2627
* [Idempotent](#idempotent)
@@ -176,6 +177,37 @@ const floorAndToString = compose((val) => val.toString(), Math.floor) // Usage
176177
floorAndToString(121.212121) // '121'
177178
```
178179

180+
## Continuation
181+
182+
At any given point in a program, the part of the code that's yet to be executed is known as a continuation.
183+
184+
```js
185+
const printAsString = (num) => console.log(`Given ${num}`)
186+
187+
const addOneAndContinue = (num, cc) => {
188+
const result = num + 1
189+
cc(result)
190+
}
191+
192+
addOneAndContinue(2, printAsString) // 'Given 3'
193+
```
194+
195+
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.
196+
197+
```js
198+
const continueProgramWith = (data) => {
199+
// Continues program with data
200+
}
201+
202+
readFileAsync('path/to/file', (err, response) => {
203+
if (err) {
204+
// handle error
205+
return
206+
}
207+
continueProgramWith(response)
208+
})
209+
```
210+
179211
## Purity
180212

181213
A function is pure if the return value is only determined by its

0 commit comments

Comments
 (0)