-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1-violation.js
More file actions
58 lines (51 loc) · 1.27 KB
/
1-violation.js
File metadata and controls
58 lines (51 loc) · 1.27 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
'use strict';
class PaymentService {
constructor(type) {
this.type = type;
}
processPayment({ to, amount, check }, callback) {
if (check) {
if (typeof amount !== 'number') {
callback(new Error('Amount expected to be number'));
}
if (amount <= 0) {
callback(new Error('Amount should be greater than zero'));
}
}
console.log(`Payment: ${amount} to ${to} by ${this.type}`);
callback(null);
}
}
class Bank extends PaymentService {
constructor() {
super();
this.active = true;
}
async processPayment({ to, amount }) {
const { promise, resolve, reject } = Promise.withResolvers();
if (this.active) {
super.processPayment({ to, amount }, resolve);
} else {
reject(new Error('Service is not active'));
}
return promise;
}
}
// Usage
const service = new PaymentService('wire');
console.log(service);
service.processPayment(
{ to: 'contractor', amount: 1000, check: true },
(error) => {
if (error) console.error(error);
else console.log('Success!\n');
},
);
const bank = new Bank();
console.log(bank);
bank
.processPayment({ to: 'contractor', amount: 1000, check: true })
.catch((error) => {
if (error) console.error(error);
else console.log('Success!');
});