forked from lowbyteproductions/Promises-From-Scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
206 lines (165 loc) · 4.95 KB
/
index.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const states = {
PENDING: 'pending',
FULFILLED: 'fulfilled',
REJECTED: 'rejected'
};
const isThenable = maybePromise => maybePromise && typeof maybePromise.then === 'function';
class LLJSPromise {
constructor(computation) {
this._state = states.PENDING;
this._value = undefined;
this._reason = undefined;
this._thenQueue = [];
this._finallyQueue = [];
if (typeof computation === 'function') {
setTimeout(() => {
try {
computation(
this._onFulfilled.bind(this),
this._onRejected.bind(this)
);
} catch (ex) {
this._onRejected(ex);
}
});
}
}
then(fulfilledFn, catchFn) {
const controlledPromise = new LLJSPromise();
this._thenQueue.push([controlledPromise, fulfilledFn, catchFn]);
if (this._state === states.FULFILLED) {
this._propagateFulfilled();
} else if (this._state === states.REJECTED) {
this._propagateRejected();
}
return controlledPromise;
}
catch(catchFn) {
return this.then(undefined, catchFn);
}
finally(sideEffectFn) {
if (this._state !== states.PENDING) {
sideEffectFn();
return this._state === states.FULFILLED
? LLJSPromise.resolve(this._value)
: LLJSPromise.reject(this._reason)
}
const controlledPromise = new LLJSPromise()
this._finallyQueue.push([controlledPromise, sideEffectFn]);
return controlledPromise;
}
_propagateFulfilled() {
this._thenQueue.forEach(([controlledPromise, fulfilledFn]) => {
if (typeof fulfilledFn === 'function') {
const valueOrPromise = fulfilledFn(this._value);
if (isThenable(valueOrPromise)) {
valueOrPromise.then(
value => controlledPromise._onFulfilled(value),
reason => controlledPromise._onRejected(reason)
);
} else {
controlledPromise._onFulfilled(valueOrPromise);
}
} else {
return controlledPromise._onFulfilled(this._value);
}
});
this._finallyQueue.forEach(([controlledPromise, sideEffectFn]) => {
sideEffectFn();
controlledPromise._onFulfilled(this._value);
});
this._thenQueue = [];
this._finallyQueue = [];
}
_propagateRejected() {
this._thenQueue.forEach(([controlledPromise, _, catchFn]) => {
if (typeof catchFn === 'function') {
const valueOrPromise = catchFn(this._reason);
if (isThenable(valueOrPromise)) {
valueOrPromise.then(
value => controlledPromise._onFulfilled(value),
reason => controlledPromise._onRejected(reason)
);
} else {
controlledPromise._onFulfilled(valueOrPromise);
}
} else {
return controlledPromise._onRejected(this._reason);
}
});
this._finallyQueue.forEach(([controlledPromise, sideEffectFn]) => {
sideEffectFn();
controlledPromise._onRejected(this._value);
});
this._thenQueue = [];
this._finallyQueue = [];
}
_onFulfilled(value) {
if (this._state === states.PENDING) {
this._state = states.FULFILLED;
this._value = value;
this._propagateFulfilled();
}
}
_onRejected(reason) {
if (this._state === states.PENDING) {
this._state = states.REJECTED;
this._reason = reason;
this._propagateRejected();
}
}
}
LLJSPromise.resolve = value => new LLJSPromise(resolve => resolve(value));
LLJSPromise.reject = value => new LLJSPromise((_, reject) => reject(value));
const fs = require('fs');
const path = require('path');
const readFile = (filename, encoding) => new LLJSPromise((resolve, reject) => {
fs.readFile(filename, encoding, (err, value) => {
if (err) {
return reject(err);
}
resolve(value);
})
});
const delay = (timeInMs, value) => new LLJSPromise(resolve => {
setTimeout(() => {
resolve(value);
}, timeInMs);
});
const asyncFn = promiseGeneratorFn => (...args) => {
const producer = promiseGeneratorFn(...args);
const interpreter = (lastValue, wasError) => {
const {value, done} = (wasError)
? producer.throw(lastValue)
: producer.next(lastValue);
if (!done) {
if (isThenable(value)) {
return value.then(
resolvedValue => interpreter(resolvedValue),
rejectedValue => interpreter(rejectedValue, true)
);
} else {
return interpreter(value);
}
} else {
if (!isThenable(value)) {
return LLJSPromise.resolve(value);
}
return value;
}
}
return interpreter();
}
const doAsyncStuff = asyncFn(function* () {
try {
const text = yield readFile(path.join(__dirname, 'indexxxx.js'), 'utf8');
console.log(`${text.length} characters read`);
const withoutVowels = yield delay(2000, text.replace(/[aeiou]/g, ''));
console.log(withoutVowels.slice(0, 200));
} catch (err) {
console.error('An error occured!');
console.error(err);
}
console.log('--- All done! ---');
});
doAsyncStuff();