-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-continuation.js
42 lines (33 loc) · 952 Bytes
/
node-continuation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/** @module */
"use static"
const Monad = require("./monad")
const { wrap } = require("./utils")
/**
* @alias module:lib/node-continuation.NodeContinuation
* @description node continuaton monad
*/
class NodeContinuationMonad extends Array {
constructor(err=null, value=null) {
super(err, value)
}
static return(value) {
return new NodeContinuationMonad(null, value)
}
static fail(err) {
return new NodeContinuationMonad(err, null)
}
static join(stack, nodeCb, cb) {
nodeCb((err, ...args) => {
if (!err) {
const { doBlock } = stack[0]
cb(doBlock.next(wrap(args.length > 1 ? args : args[0], stack.length)))
} else {
const { doBlock } = stack[stack.length - 1]
const value = NodeContinuationMonad.fail(err)
doBlock.next({ value, done: true })
cb(stack[0].doBlock.next({ value, done: false }))
}
})
}
}
module.exports = NodeContinuationMonad