-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathunhandled_rejection.cpp
More file actions
79 lines (65 loc) · 1.94 KB
/
Copy pathunhandled_rejection.cpp
File metadata and controls
79 lines (65 loc) · 1.94 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
#include "quickjspp.hpp"
#include <iostream>
#include <stdexcept>
int main()
{
qjs::Runtime runtime;
qjs::Context context(runtime);
context.global()["nextTick"] = [&context](std::function<void()> f) {
context.enqueueJob(std::move(f));
};
bool called = false;
context.onUnhandledPromiseRejection = [&called](qjs::Value reason) {
called = true;
};
// Catch the rejection
called = false;
context.eval(R"xxx(
(() => {
const p = new Promise((resolve, reject) => {
nextTick(() => {
reject(123);
})
});
p.catch(() => {/* no-op */});
})();
)xxx");
while (runtime.isJobPending()) {
runtime.executePendingJob();
}
assert(!called && "Unhandled Promise rejection should not have been called");
// Catch the rejection, but only one *after* it happens.
called = false;
context.eval(R"xxx(
(() => {
const p = new Promise((resolve, reject) => {
reject(123);
nextTick(() => {
p.catch(() => {/* no-op */});
});
});
})();
)xxx");
while (runtime.isJobPending()) {
runtime.executePendingJob();
}
assert(!called && "Unhandled Promise rejection should not have been called");
// Do not catch the rejection
called = false;
context.eval(R"xxx(
(() => {
const p = new Promise((resolve, reject) => {
reject(123);
nextTick(() => {
p.then(() => {/* no-op */})
});
});
})();
)xxx");
assert(!called && "Unhandled Promise rejection should not have been called yet");
while (runtime.isJobPending()) {
runtime.executePendingJob();
}
assert(called && "Unhandled Promise rejection should have been called");
return 0;
}