-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
46 lines (38 loc) · 1.07 KB
/
promise.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
43
44
45
46
/** @module */
"use static"
const Monad = require("./monad")
const { wrap } = require("./utils")
/**
* @class
* @alias module:lib/promise.PromiseMonad
* @description The Promise Monad.
*/
const PromiseMonad = {
__proto__: Monad,
return(value) {
return Promise.resolve(value)
},
fail(value) {
return Promise.reject(value)
},
join(stack, promise, cb) {
// console.log(`PromiseMonad: ${Array.isArray(stack)} ${promise instanceof Promise}`)
promise
.then(value => {
const { doBlock } = stack[0]
// console.log(`PromiseMonad: then: doBlock.next(${JSON.stringify(wrap(value, stack.length))})`)
cb(doBlock.next(wrap(value, stack.length)))
})
.catch(err => {
const { doBlock } = stack[stack.length - 1]
/*
* console.log(`PromiseMonad: catch: ${JSON.stringify(err)}`)
* console.log(err)
*/
const value = this.fail(err)
doBlock.next({ value, done: true })
cb(stack[0].doBlock.next({ value, done: false }))
})
},
}
module.exports = PromiseMonad