forked from Azure/login
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoPromise.js
More file actions
178 lines (176 loc) · 4.65 KB
/
Copy pathNoPromise.js
File metadata and controls
178 lines (176 loc) · 4.65 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* This is not a `Promise`.
*
* Chain callback functions with `.then(function (res, cb))` and execute them
* as soon as previous callbacks have finished.
*
* Catch passed or thrown errors with `.catch(function (err, res, cb))` as they may occur.
* End the chain with `.end(function (err, res))`.
*
* If errors are thrown inside a `task` they are catched and can be processed attaching
* `.catch()` or `.end()` to the chain.
*
* This method is similar to {@link module:serial.connect|connect} but allows adding `tasks` on the go through chaining.
*
* @name NoPromise
* @class
* @param {Any} arg - initial argument which is passed to first chain
* @example <caption>Normal usage</caption>
* var arr = []
* var n = new NoPromise(arr)
* n.then((res, cb) => {
* res.push(1)
* cb(null, res)
* }).then((res, cb) => {
* res.push(2)
* cb(null, res)
* }).end((err, res) => {
* //> err = null
* //> res = [1, 2]
* //> (arr ==== res) = true
* })
* @example <caption>Catch errors</caption>
* var arr = []
* var n = new NoPromise(arr)
* n.then((res, cb) => {
* res.push(1)
* cb(null, res)
* }).then((res, cb) => {
* res.push(2)
* cb('err1', res) // <-- cause an error
* }).catch((err, res, cb) => { // catches err1
* res.push(err)
* cb(null, res) // <-- continue normally
* }).then((res, cb) => {
* res.push(3)
* cb(null, res)
* }).catch((err, res, cb) => { // jumps over, as there is no error in the chain
* res.push(4)
* cb(null, res)
* }).then((res, cb) => {
* res.push(5)
* cb('err2', res) // <-- next error
* }).end((err, res) => {
* //> err = 'err2'
* //> res = [1, 2, 'err1', 3, 5]
* //> (arr ==== res) = true
* })
* @example <caption>Deferred usage</caption>
* var arr = []
* // creates a new instance passing `arr`
* var n = new NoPromise(arr)
* // execute the first async method
* n.then((res, cb) => {
* res.push(1)
* cb(null, res)
* })
* // take a time off
* setTimeout(() => {
* // continue processing
* n.then((res, cb) => {
* res.push(2)
* cb(null, res)
* }).end((err, res) => {
* //> err = null
* //> res = [1, 2]
* //> (arr ==== res) = true
* })
* }, 10)
*/
export default function NoPromise (arg) {
this._tasks = []
this.result = arg
this.error = undefined
this._lock = false
}
NoPromise.prototype = {
/**
* runs the next function
* @private
*/
_run: function () {
if (this._lock) return
this._lock = true
let task = this._tasks.shift()
const tstType = this.error ? ['catch', 'end'] : ['then', 'end']
while (task && !~tstType.indexOf(task.type)) {
task = this._tasks.shift()
}
if (task) {
const cb = (err, res) => {
this.error = err
this.result = res || this.result
this._lock = false
this._run()
}
const fn = task.fn
if (task.type === 'end') { // .end
fn(this.error, this.result)
} else {
try {
if (task.type === 'catch') { // .catch
fn(this.error, this.result, cb)
} else { // .then
fn(this.result, cb)
}
} catch (e) {
cb(e)
}
}
} else {
this._lock = false
}
},
/**
* Chain the next async function
* @param {Function} task - async function `function (res: any, cb: Function)`.
* Never forget to call `cb(err: <Error>, res: any)` inside `fn`
*/
then: function (task) {
this._tasks.push({ type: 'then', fn: task })
this._run()
return this
},
/**
* Catch any previous errors from the chain
* @param {Function} trap - async function `function (err: <Error>, res: any, cb: Function)`.
* Never forget to call `cb(err: <Error>, res: any)` inside `fn`
*/
catch: function (trap) {
this._tasks.push({ type: 'catch', fn: trap })
this._run()
return this
},
/**
* End the chain
* @param {Function} callback - `function (err: <Error>, res: any)`
*/
end: function (callback) {
this._tasks.push({ type: 'end', fn: callback })
this._run()
}
}
/**
* This is not a `Promise`.
*
* Chain callback functions with `.then(function (res, cb))` and execute them
* as soon as previous callbacks have finished.
*
* Catch passed or thrown errors with `.catch(function (err, res, cb))` as they may occur.
* End the chain with `.end(function (err, res))`.
*
* If errors are thrown inside a `task` they are catched and can be processed attaching
* `.catch()` or `.end()` to the chain.
*
* See full API here {@link NoPromise}.
*
* @name noPromise
* @memberOf module:serial
* @static
* @method
* @param {Any} arg - initial argument which is passed to first chain
* @return {NoPromise}
*/
export function noPromise (arg) {
return new NoPromise(arg)
}