Skip to content

Commit 8b66cad

Browse files
committed
Promises will reject on weekends
1 parent cae4dc7 commit 8b66cad

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

JavaScript/6-promise-sequential.js

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,45 @@
22

33
// Emulate Asynchronous calls
44

5-
function wrapAsync(callback) {
6-
setTimeout(callback, Math.floor((Math.random() * 1000)));
7-
}
5+
const wrapAsync = (callback) => setTimeout(
6+
callback, Math.floor((Math.random() * 1000))
7+
);
8+
9+
const isWeekend = () => !(new Date().getDay() % 6);
810

911
// Asynchronous functions
1012

11-
function readConfig(name) {
12-
return new Promise((resolve, reject) => {
13-
wrapAsync(() => {
14-
console.log('(1) config loaded');
15-
resolve({ name });
16-
// reject(new Error('Promise fails'));
17-
});
13+
const readConfig = (name) => new Promise((resolve, reject) => {
14+
wrapAsync(() => {
15+
console.log('(1) config loaded');
16+
if (!isWeekend()) resolve({ name });
17+
else reject(new Error('Promises will resolve next working day'));
1818
});
19-
}
20-
21-
function doQuery(statement) {
22-
return new Promise((resolve, reject) => {
23-
wrapAsync(() => {
24-
console.log('(2) SQL query executed: ' + statement);
25-
resolve([ { name: 'Kiev' }, { name: 'Roma' } ]);
26-
});
19+
});
20+
21+
const doQuery = (statement) => new Promise((resolve, reject) => {
22+
wrapAsync(() => {
23+
console.log('(2) SQL query executed: ' + statement);
24+
if (!isWeekend()) resolve([{ name: 'Kiev' }, { name: 'Roma' }]);
25+
else reject(new Error('Promises will resolve next working day'));
2726
});
28-
}
29-
30-
function httpGet(url) {
31-
return new Promise((resolve, reject) => {
32-
wrapAsync(() => {
33-
console.log('(3) Page retrieved: ' + url);
34-
resolve('<html>Some archaic web here</html>');
35-
});
27+
});
28+
29+
const httpGet = (url) => new Promise((resolve, reject) => {
30+
wrapAsync(() => {
31+
console.log('(3) Page retrieved: ' + url);
32+
if (!isWeekend()) resolve('<html>Some archaic web here</html>');
33+
else reject(new Error('Promises will resolve next working day'));
3634
});
37-
}
38-
39-
function readFile(path) {
40-
return new Promise((resolve, reject) => {
41-
wrapAsync(() => {
42-
console.log('(4) Readme file loaded: ' + path);
43-
resolve('file content');
44-
});
35+
});
36+
37+
const readFile = (path) => new Promise((resolve, reject) => {
38+
wrapAsync(() => {
39+
console.log('(4) Readme file loaded: ' + path);
40+
if (!isWeekend()) resolve('file content');
41+
else reject(new Error('Promises will resolve next working day'));
4542
});
46-
}
43+
});
4744

4845
// Usage
4946

0 commit comments

Comments
 (0)