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,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);