-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
141 lines (120 loc) · 2.78 KB
/
promise.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
/**
* @param somebody
*/
const EventEmitter = function() {
this._eventsCallbacks = {};
};
/**
* @param somebody
*/
EventEmitter.prototype.trigger = function(event) {
const eventCallbacks = this._eventsCallbacks[event];
if (!eventCallbacks) {
return;
}
eventCallbacks.forEach(function(func){
func();
})
}
/**
* @param somebody
*/
EventEmitter.prototype.on = function (event, callback) {
if (!this._eventsCallbacks[event]) {
this._eventsCallbacks[event] = [];
}
this._eventsCallbacks[event].push(callback);
}
/**
* @param somebody
*/
const Promise2 = function(f1) {
EventEmitter.call(this);
// pending, fulfilled, rejected
this._status = 'pending';
this._result = null;
const reject = () => {
this._setStatus('rejected');
return this;
}
const approove = (result) => {
const isResultPromise = result instanceof Promise2;
if (isResultPromise) {
result.then((newResult) => {
this._updateStatePromise(newResult);
});
return;
}
this._updateStatePromise(result);
}
f1(approove, reject.bind(this));
};
/**
* @param somebody
*/
Promise2.prototype = Object.create(EventEmitter.prototype);
Promise2.prototype._updateStatePromise = function(result) {
this._result = result;
this._setStatus('fulfilled');
return this;
}
/**
* @param somebody
*/
Promise2.prototype._setStatus = function(status) {
this._status = status;
this.trigger('changeStatus');
}
/**
* Добавляет обработчик выполнения и отклонения обещания
*
* @callback коллбек промиса
* @param {*}
*/
Promise2.prototype.then = function(callback) {
// Оборачиваем колбек в функцию для передачи в промис
const extendedCallback = (approove, reject) => {
if (this._status === "fulfilled") {
// @todo: нужно заложиться, если коллбек асинхронный
const result = callback(this._result);
approove(result);
}
this.on('changeStatus', () => {
approove(callback(this._result));
});
};
return new Promise2(extendedCallback);
}
/*
* Examples
*/
var func1 = function(approove, reject) {
setTimeout(() => {
approove('ready#1');
}, 200)
}
var func2 = function(approove, reject) {
setTimeout(() => {
approove('ready#2');
}, 2000)
}
var prom = new Promise2(func1)
prom.constructor.name;
// prom.then(function(result) {
// setTimeout(() => {
// console.log('ready#1-after', result);
// }, 2000)
// return new Promise2(func2);
// }).then(function(result) {
// console.log('ready#2-after', result);
// return 'before3';
// }).then(function(result) {
// console.log('ready#3', result);
// });
// console.log('after-prom')
const promise = new Promise2(func1);
setTimeout(() => {
promise.then((res) => {
console.log(res);
});
}, 1000);