-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
99 lines (78 loc) · 2.23 KB
/
main.cpp
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
#include <iostream>
#include "Promise.cc"
class Error{ // class for exception-handling
std::string message;
public:
Error()=default;
Error(std::string message) : message{message}{
}
std::string show(){
return message;
}
};
std::ostream&operator<<(std::ostream&out, Error err){
out << err.show();
return out;
}
// some of the examples taken from <url> understanding-javascript-promises </url>
void e1(){
auto a1 = ECMA::Promise<int, int>([](auto resolve){
resolve(5101);
});
auto a2 = ECMA::Promise<int>([](auto resolve, auto reject){
reject(11);
});
a1.then([](auto resolve, auto reject){
std::cout << resolve << std::endl;
});
a2.then([](auto resolve, auto reject){
std::cout << reject << std::endl;
});
a1.then([](auto resolve){ // not able to "resolve" a Promise a second time
std::cout << "S: " << resolve << std::endl;
});
// or
ECMA::Promise<int, int>::resolve(5101).then([](auto resolve, auto reject){
std::cout << resolve << std::endl;
});
ECMA::Promise<int>::reject(11).then([](auto resolve, auto reject){
std::cout << reject << std::endl;
});
// or even
ECMA::Promise<int, int>::resolve(50).await(); // 50
// how to use async
ECMA::Promise<int>::async((std::function<int(int)>)[](auto v){
return v;
}, 5).then([](auto resolve){
std::cout << resolve << std::endl;
});
}
void e2(){ // handling errors
ECMA::Promise<std::string, Error>([](auto resolve, auto reject){
throw Error("Error");
})._catch([](auto err){
std::cout << err << std::endl;
});
// or
ECMA::Promise<std::string>([](auto resolve, auto reject){
reject("Error");
})._catch([](auto err){
std::cout << err << std::endl;
});
}
void e3(){ // cascading errors
ECMA::Promise<int, char*>([](auto resolve, auto reject){
throw (char*)"ERR";
})._catch([](auto err){
// std::cout << err << std::endl;
throw (char*)"SEC";
})._catch([](auto err){
std::cout << err << std::endl;
});
}
int main(void){
e1();
e2();
e3();
return 0;
};