-
Notifications
You must be signed in to change notification settings - Fork 0
/
babyPromise.js
55 lines (38 loc) · 1.48 KB
/
babyPromise.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
// Description: babyPromise is a sort of light version of Javascript Promises
// Created by: Eduardo Zola
// Email me at: [email protected]
// License: GNU General Public License v3.0 - Permissions of this strong copyleft license are conditioned on making available complete source code of licensed works and modifications, which include larger works using a licensed work, under the same license. Copyright and license notices must be preserved. Contributors provide an express grant of patent rights.
function babyPromise(userInitFunc,autoRun = true) {
this.ret = null;
this.status = 0; // 0 = pending, 1 = running, 2 = resolved, 3 = rejected
this.autoRun = autoRun;
this.funcThen = [];
this.funcCatch = function() {};
this.resolve = async function(p) {
this.ret = p;
this.status=2;
for(this.i=0;this.i<this.funcThen.length;this.i++) {
this.ret = await this.funcThen[this.i](this.ret);
}
}
this.reject = function(p) {
this.ret = p;
this.status=3;
this.funcCatch(this.ret);
}
this.then = async function(p) {
this.funcThen.push(p);
}
this.catch = async function(p) {
this.funcCatch = p;
}
this.userInitFunc = userInitFunc;
this.run = async function(...p) {
if(this.status == 0) {
this.status = 1;
this.userInitFunc(...p);
}
}
if(this.autoRun) this.run();
}
module.exports = babyPromise;