-
Notifications
You must be signed in to change notification settings - Fork 1
/
promdash.src.js
37 lines (33 loc) · 954 Bytes
/
promdash.src.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
import _ from 'lodash'
export default class Promdash extends Promise {
then (resolve, reject) {
const arrayResolution = (...args) => {
if (resolve) {
const result = resolve(...args)
return Array.isArray(result) ? this.constructor.all(result) : result
}
}
return super.then(arrayResolution, reject)
}
}
Promdash.from = function (promise) {
if (!promise.then) {
throw new Error('Promdash.from() requires a `then`able object')
}
const P = this || Promdash
return new P(promise.then.bind(promise))
}
_.functions(_).forEach(f => {
if (!Promdash.prototype[f]) {
Promdash.prototype[f] = function (...yargs) {
return this.then((...xargs) => _[f](...xargs, ...yargs))
}
}
if (!Promdash[f]) {
Promdash[f] = function (...args) {
const result = _[f](...args)
const P = this || Promdash
return Array.isArray(result) ? P.all(result) : P.resolve(result)
}
}
})