-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPossiblePromise.js
144 lines (128 loc) · 3.28 KB
/
PossiblePromise.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* rubico v1.5.0
* https://github.com/a-synchronous/rubico
* (c) 2019-2020 Richard Tong
* rubico may be freely distributed under the MIT license.
*/
'use strict'
const isPromise = value => value != null && typeof value.then == 'function'
const promiseAll = Promise.all.bind(Promise)
/**
* @name PossiblePromise
*
* @synopsis
* new PossiblePromise(value Promise|any) -> PossiblePromise
*
* @catchphrase
* Possibly a Promise
*/
const PossiblePromise = function(value) {
this.value = value
}
/**
* @name PossiblePromise.prototype.then
*
* @synopsis
* new PossiblePromise(value Promise|any).then(func function) -> Promise|any
*
* @catchphrase
* .then if internal Promise, else call
*/
PossiblePromise.prototype.then = function(func) {
const value = this.value
return isPromise(value) ? value.then(func) : func(value)
}
/**
* @name PossiblePromise.then
*
* @synopsis
* PossiblePromise.then(value Promise|any, func function) -> Promise|any
*
* @catchphrase
* .then if passed a Promise, else call
*/
PossiblePromise.then = (value, func) => (
isPromise(value) ? value.then(func) : func(value))
/**
* @name PossiblePromise.catch
*
* @synopsis
* PossiblePromise.catch(
* value Promise|any,
* func any=>Promise|any,
* ) -> Promise|any
*
* @catchphrase
* .catch if passed a Promise, else noop
*/
PossiblePromise.catch = (value, func) => (
isPromise(value) ? value.catch(func) : value)
/**
* @name SyncThenable
*
* @synopsis
* new SyncThenable(value any) -> SyncThenable
*
* @catchphrase
* A synchronous Promise-like structure
*/
const SyncThenable = function(value) { this.value = value }
/**
* @name SyncThenable.prototype.then
*
* @synopsis
* new SyncThenable(value any).then(func function) -> any
*
* @catchphrase
* .then as a function call
*/
SyncThenable.prototype.then = function(func) { return func(this.value) }
/**
* @name PossiblePromise.all
*
* @synopsis
* PossiblePromise.all(
* values Array<Promise>|Array,
* ) -> Promise<Array>|PossiblePromise<Array>
*
* @catchphrase
* Always returns a thenable of an Array
*/
PossiblePromise.all = values => (values.some(isPromise)
? promiseAll(values)
: new SyncThenable(values))
/**
* @name PossiblePromise.args
*
* @synopsis
* PossiblePromise.args(func function)(args ...any) -> Promise|any
*
* @catchphrase
* Resolves any Promises supplied as arguments to a function
*/
PossiblePromise.args = func => (...args) => (args.some(isPromise)
? promiseAll(args).then(res => func(...res))
: func(...args))
/* PossiblePromise.argsSome = func => (...args) => (args.some(isPromise)
? promiseAll(args).then(res => func(...res))
: func(...args))
PossiblePromise.argsSomeApply = func => {
const fApply = func.apply.bind(func)
return (...args) => (args.some(isPromise)
? promiseAll(args).then(res => fApply(null, res))
: fApply(null, args))
}
PossiblePromise.argumentsLoop = func => function() {
for (let i = 0; i < arguments.length; i++) {
if (isPromise(arguments[i])) {
return promiseAll(arguments).then(res => func(...res))
}
}
return func.apply(null, arguments)
}
PossiblePromise.argsLoop = func => (...args) => {
for (const arg of args) {
if (isPromise(arg)) return promiseAll(args).then(res => func(...res))
}
return func(...args)
} */
module.exports = PossiblePromise