Skip to content

Commit b8d5028

Browse files
committed
continuations
1 parent 02b0546 commit b8d5028

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
@@ -20,6 +20,7 @@ __Table of Contents__
2020
* [Currying](#currying)
2121
* [Auto Currying](#auto-currying)
2222
* [Function Composition](#function-composition)
23+
* [Continuation](#continuation)
2324
* [Purity](#purity)
2425
* [Side effects](#side-effects)
2526
* [Idempotent](#idempotent)
@@ -175,6 +176,37 @@ const floorAndToString = compose((val) => val.toString(), Math.floor) // Usage
175176
floorAndToString(121.212121) // '121'
176177
```
177178

179+
## Continuation
180+
181+
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.
182+
183+
```js
184+
const printAsString = (num) => console.log(`Given ${num}`)
185+
186+
const addOneAndContinue = (num, cc) => {
187+
const result = num + 1
188+
cc(result)
189+
}
190+
191+
addOneAndContinue(2, printAsString) // 'Given 3'
192+
```
193+
194+
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.
195+
196+
```js
197+
const continueProgramWith = (data) => {
198+
// Continues program with data
199+
}
200+
201+
readFileAsync('path/to/file', (err, response) => {
202+
if (err) {
203+
// handle error
204+
return
205+
}
206+
continueProgramWith(response)
207+
})
208+
```
209+
178210
## Purity
179211

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

0 commit comments

Comments
 (0)