forked from babel/babel
/
exec.js
51 lines (42 loc) · 985 Bytes
/
exec.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
function mandatory(paramName) {
throw new Error(`Missing parameter: ${paramName}`);
}
function shouldNotHappen() {
throw new Error("should not happen");
}
async function foo({ a, b = mandatory("b") } = {}) {
return Promise.resolve(b);
}
async function bar(a = mandatory("a"), b = mandatory("b")) {
return Promise.resolve(b);
}
assert.doesNotThrow(() => {
foo().then(() => true).catch(() => true);
});
assert.doesNotThrow(() => {
bar().then(() => true).catch(() => true);
});
async function fooUsage() {
try {
await foo().then(shouldNotHappen);
} catch (err) {
assert.equal(err.message, "Missing parameter: b");
}
};
async function barUsage() {
try {
await bar().then(shouldNotHappen);
} catch (err) {
assert.equal(err.message, "Missing parameter: a");
}
}
fooUsage().catch((err) => {
if (err instanceof AssertionError) {
throw err;
}
});
barUsage().catch((err) => {
if (err instanceof AssertionError) {
throw err;
}
});