new class js -advanced
This commit is contained in:
@@ -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)
|
||||
);
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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...
|
||||
@@ -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);
|
||||
@@ -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.'));
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
@@ -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}`));
|
||||
@@ -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}`));
|
||||
@@ -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}`));
|
||||
@@ -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