new class js -advanced
This commit is contained in:
@@ -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);
|
||||
// }
|
||||
// });
|
||||
@@ -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);
|
||||
// }
|
||||
// });
|
||||
@@ -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);
|
||||
// }
|
||||
// });
|
||||
@@ -0,0 +1 @@
|
||||
This is the contents of file 01.
|
||||
@@ -0,0 +1 @@
|
||||
This is the contents of file 02.
|
||||
@@ -0,0 +1 @@
|
||||
This is the contents of file 03.
|
||||
@@ -0,0 +1 @@
|
||||
This is the contents of file 04.
|
||||
@@ -0,0 +1 @@
|
||||
This is the contents of file 05.
|
||||
@@ -0,0 +1 @@
|
||||
This is the contents of file 06.
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user