mirror of
https://github.com/rjNemo/functional-programming-jargon
synced 2026-06-06 02:26:43 +00:00
continuations
This commit is contained in:
parent
02b0546772
commit
b8d5028816
1 changed files with 32 additions and 0 deletions
32
readme.md
32
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue