new class js -advanced

This commit is contained in:
Philippe Torrel
2026-06-29 11:35:03 +02:00
parent ee8a87bf2a
commit 31c13250b0
104 changed files with 2632 additions and 138 deletions

View File

@@ -0,0 +1,15 @@
const tryWork = (resolve, reject) => {
const ergebnis = 21 * 2;
if (Math.random() < 0.5) {
resolve(ergebnis);
} else {
reject('nope');
}
};
const p4 = new Promise(tryWork);
p4.then(
(result) => console.log('OK: ' + result),
(reason) => console.log('KO: ' + reason)
);

View File

@@ -0,0 +1,17 @@
const tryWork = (resolve, reject) => {
const ergebnis = 21 * 2;
if (Math.random() < 0.5) {
resolve(ergebnis);
} else {
reject(new Error('nope'));
}
};
new Promise(tryWork)
.then(() => new Promise(tryWork))
.then(() => new Promise(tryWork))
.then((result) => console.log('OK: ' + result))
.catch((err) => {
console.log('KO: ' + err.message);
console.log(err.stack);
});

View File

@@ -0,0 +1,12 @@
const wait = () => new Promise((resolve) => setTimeout(resolve, 1000));
wait()
.then(() => {
console.log('The');
return wait();
})
.then(() => {
console.log('pyramid');
return wait();
});
// etc...

View File

@@ -0,0 +1,26 @@
// print a phrase, a word at a time:
// the pyramid of doom appears
setTimeout(() => {
console.log('The');
setTimeout(() => {
console.log('pyramid');
setTimeout(() => {
console.log('of');
setTimeout(() => {
console.log('doom');
setTimeout(() => {
console.log('keeps');
setTimeout(() => {
console.log('growing.');
}, 1000);
}, 1000);
}, 1000);
}, 1000);
}, 1000);
}, 1000);

View File

@@ -0,0 +1,17 @@
// print a phrase, a word at a time:
// use promises to avoid the pyramid of doom
const printDelay = (time, str) =>
new Promise((resolve) =>
setTimeout(() => {
console.log(str);
resolve();
}, time)
);
printDelay(1000, 'The')
.then(() => printDelay(1000, 'pyramid'))
.then(() => printDelay(1000, 'of'))
.then(() => printDelay(1000, 'doom'))
.then(() => printDelay(1000, 'is'))
.then(() => printDelay(1000, 'defeated.'));

View File

@@ -0,0 +1,9 @@
const fs = require('fs');
fs.readFile('data/file.txt', 'UTF8', (error, content) => {
if (error) {
console.log('could not read file');
} else {
console.log(content);
}
});

View File

@@ -0,0 +1,17 @@
const fs = require('fs');
const getFileContent = (path, encoding = 'utf-8') => {
return new Promise((resolve, reject) => {
fs.readFile(path, encoding, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
};
getFileContent('data/file.txt')
.then((data) => console.log(data))
.catch((err) => console.error(`Something went wrong: ${err}`));

View File

@@ -0,0 +1,9 @@
const fs = require('fs');
const getFileContent = (path, encoding = 'utf-8') => {
return fs.promises.readFile(path, encoding);
};
getFileContent('data/file.txt')
.then((data) => console.log(data))
.catch((err) => console.error(`Something went wrong: ${err}`));

View File

@@ -0,0 +1,8 @@
const fs = require('fs');
const util = require('util');
const getFileContent = util.promisify(fs.readFile);
getFileContent('data/file.txt', 'UTF8')
.then((data) => console.log(data))
.catch((err) => console.error(`Something went wrong: ${err}`));

View File

@@ -0,0 +1,29 @@
const dns = require('dns');
const IP_V = 4; // we use IP protocol version 4
const URL = 'de.webmasters-europe.org';
// Solution 1: Detailed variant with Promise object
const getIp = (url, ip) => {
return new Promise((resolve, reject) => {
dns.lookup(url, ip, (error, data) => {
if (error) {
reject(error);
} else {
resolve({ address: data, family: ip });
}
});
});
};
getIp(URL, IP_V)
.then((data) => console.log(`IP adress = ${data.address}`))
.catch((err) => console.error(err));
// dns.lookup(URL, IP_V, (error, address) => {
// if (error) {
// console.log('error: could not lookup host');
// } else {
// console.log('IP address = ' + address);
// }
// });

View File

@@ -0,0 +1,21 @@
const dns = require('dns');
const IP_V = 4; // we use IP protocol version 4
const URL = 'de.webmasters-europe.org';
// Solution 2: Variant with promises subobject
const getIpPromises = (url, ip) => {
return dns.promises.lookup(url, ip);
};
getIpPromises(URL, IP_V)
.then((data) => console.log(`IP adress = ${data.address}`))
.catch((err) => console.error(err));
// dns.lookup(URL, IP_V, (error, address) => {
// if (error) {
// console.log('error: could not lookup host');
// } else {
// console.log('IP address = ' + address);
// }
// });

View File

@@ -0,0 +1,20 @@
const dns = require('dns');
const util = require('util');
const IP_V = 4; // we use IP protocol version 4
const URL = 'de.webmasters-europe.org';
// Solution 3: Variant with util module
const getIpPromisify = util.promisify(dns.lookup);
getIpPromisify(URL, IP_V)
.then((data) => console.log(`IP adress = ${data.address}`))
.catch((err) => console.error(err));
// dns.lookup(URL, IP_V, (error, address) => {
// if (error) {
// console.log('error: could not lookup host');
// } else {
// console.log('IP address = ' + address);
// }
// });

View File

@@ -0,0 +1 @@
This is the contents of file 01.

View File

@@ -0,0 +1 @@
This is the contents of file 02.

View File

@@ -0,0 +1 @@
This is the contents of file 03.

View File

@@ -0,0 +1 @@
This is the contents of file 04.

View File

@@ -0,0 +1 @@
This is the contents of file 05.

View File

@@ -0,0 +1 @@
This is the contents of file 06.

View File

@@ -0,0 +1,30 @@
const fs = require('fs');
const getFileContent = (name, encoding = 'UTF8') => {
return new Promise((resolve, reject) => {
fs.readFile(name, encoding, (error, content) => {
if (error) {
reject(`could not read file ${name}`);
} else {
resolve(content);
}
});
});
};
Promise.all([
getFileContent('data/file_1.txt'),
getFileContent('data/file_2.txt'),
getFileContent('data/file_3.txt'),
getFileContent('data/file_4.txt'),
getFileContent('data/file_5.txt'),
getFileContent('data/file_6.txt'),
])
.then((contents) =>
contents.forEach((content) => {
console.log(content);
})
)
.catch((error) => {
console.log(error);
});

View File

@@ -0,0 +1,18 @@
// print a phrase, a word at a time:
// workaround to avoid the pyramid of doom
const words = 'The pyramid of doom keeps growing.';
const printWords = (str = '', idx = 0) => {
const words = str.split(/\s/);
// Guard
if (idx >= words.length) return '';
console.log(words[idx]);
setTimeout(() => {
printWords(str, ++idx);
}, 1000);
};
printWords(words);