-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathipromise.ts
More file actions
115 lines (101 loc) · 2.21 KB
/
ipromise.ts
File metadata and controls
115 lines (101 loc) · 2.21 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
type state = 'pending' | 'fulfilled' | 'rejected';
type resolve = number | string | boolean | symbol | object | IPromise;
interface iPromise {
then(resolve: <T>(resolve: T) => T, reject?: Function): IPromise;
}
class IPromise implements iPromise {
private _state: state;
private _rejected: string;
private _value: resolve;
constructor(execute: Function) {
this._state = 'pending';
this._rejected = undefined;
this._value = undefined;
let resolve = (val: resolve) => {
if (this._state === 'pending') {
this._state = 'fulfilled';
this._value = val;
}
};
let reject = (val: resolve) => {
if (this._state === 'pending') {
this._state = 'rejected';
this._value = val;
}
};
try {
execute(resolve, reject);
} catch (err) {
reject(err);
}
}
then(resolve: Function, reject?: Function): IPromise {
let _ref;
let promise2 = new IPromise(() => {});
this._state === 'fulfilled' && (_ref = resolve(this._value));
if (_ref instanceof IPromise) {
promise2._state = _ref._state;
promise2._value = _ref._value;
} else {
promise2._value = _ref;
promise2._state = 'fulfilled';
}
reject && reject(this._rejected);
return promise2;
}
}
let temp = new IPromise(resolve => {
resolve(1);
});
temp
.then(data => data * 2)
.then(data => data * 5)
.then(data => console.log(data));
interface TAbc {
getName(): string;
}
abstract class Animal {
private name: string;
constructor(name: string) {
this.name = name;
}
move(): void {
console.log(`${this.name} is move`);
}
getName(): string {
return this.name;
}
abstract bark(): void;
}
class Dog extends Animal {
constructor(name: string) {
super(name);
}
bark(): void {
console.log(`${this.getName()} bark wang wang wang`);
}
}
class Cat extends Animal {
constructor(name: string) {
super(name);
}
bark(): void {
console.log(`${this.getName()} bark miao miao miao`);
}
}
let dog = new Dog('dog');
let cat = new Cat('cat');
dog.bark();
cat.bark();
dog.move();
cat.move();
enum Color {
red,
green,
pick,
}
namespace Color {
export function minColor() {
Color.green;
}
}