This commit is contained in:
Philippe Torrel
2026-07-03 10:22:02 +02:00
parent f0dd230fe9
commit 9a1be89bd8
27 changed files with 522 additions and 10 deletions

View File

@@ -1,5 +1,8 @@
// fs = FileSystem Modul aus der Node Standardbib
const fs = require('fs'); // commonjs - Node Schreibweise
// const fs = require('fs'); // commonjs - Node Schreibweise
// const chalk = require('chalk');
import fs from 'fs'; // module - ESM Schreibweise
import chalk from 'chalk';
// Funktion mir einem asynchronen Prozess
const getContentBy = (path) => {
@@ -16,13 +19,13 @@ const getContentBy = (path) => {
});
};
// Funktionsaufruf einer Funktion mit asynchronem Prozess
getContentBy('data/productss.csv')
// Funktionsaufruf einer Funktion mit asynchronen Prozess
getContentBy('data/products.csv')
.then((data) => {
console.log(data);
console.log(chalk.yellow(data));
})
.catch((err) => {
console.error(err);
console.error(chalk.red(err));
});
console.log('weiter im code...');

View File

@@ -0,0 +1,28 @@
{
"name": "01_promise-and-readfile",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "01_promise-and-readfile",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"chalk": "^5.6.2"
}
},
"node_modules/chalk": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
"license": "MIT",
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
}
}
}

View File

@@ -4,10 +4,13 @@
"description": "",
"license": "ISC",
"author": "",
"type": "commonjs",
"type": "module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": []
"keywords": [],
"dependencies": {
"chalk": "^5.6.2"
}
}

View File

@@ -0,0 +1,27 @@
// commonjs - Schreibweise mit "require"
const fs = require('node:fs'); // "fs" ist ein Modul aus der Node Standardbib und kann mit Prefix "node:" hervorgehoben werden
const chalk = require('chalk');
const getFileBy = (path, encoding = 'utf-8') => {
return new Promise((resolve, reject) => {
// async process
fs.readFile(path, encoding, (err, data) => {
if (err) {
// console.error(err);
reject(err);
}
// console.log(data);
resolve(data);
});
});
};
// Funktionsaufruf von Funktion mit async process
getFileBy('package.json')
.then((data) => {
console.log(chalk.yellow(data));
})
.catch((err) => {
console.log(chalk.red(err));
});

View File

@@ -0,0 +1,86 @@
{
"name": "commonjs",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "commonjs",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"chalk": "^4.1.2"
}
},
"node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"license": "MIT",
"dependencies": {
"color-convert": "^2.0.1"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"license": "MIT",
"dependencies": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
},
"node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"license": "MIT",
"dependencies": {
"color-name": "~1.1.4"
},
"engines": {
"node": ">=7.0.0"
}
},
"node_modules/color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"license": "MIT"
},
"node_modules/has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"license": "MIT",
"engines": {
"node": ">=8"
}
},
"node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"license": "MIT",
"dependencies": {
"has-flag": "^4.0.0"
},
"engines": {
"node": ">=8"
}
}
}
}

View File

@@ -0,0 +1,16 @@
{
"name": "commonjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"dependencies": {
"chalk": "^4.1.2"
}
}

View File

@@ -0,0 +1,27 @@
// esm - Schreibweise mit "import"
import fs from 'node:fs'; // "fs" ist ein Modul aus der Node Standardbib und kann mit Prefix "node:" hervorgehoben werden
import chalk from 'chalk';
const getFileBy = (path, encoding = 'utf-8') => {
return new Promise((resolve, reject) => {
// async process
fs.readFile(path, encoding, (err, data) => {
if (err) {
// console.error(err);
reject(err);
}
// console.log(data);
resolve(data);
});
});
};
// Funktionsaufruf von Funktion mit async process
getFileBy('package.json')
.then((data) => {
console.log(chalk.yellow(data));
})
.catch((err) => {
console.log(chalk.red(err));
});

View File

@@ -0,0 +1,28 @@
{
"name": "module",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "module",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"chalk": "^5.6.2"
}
},
"node_modules/chalk": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
"license": "MIT",
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
}
}
}

View File

@@ -0,0 +1,16 @@
{
"name": "module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"chalk": "^5.6.2"
}
}

View File

@@ -0,0 +1,40 @@
// esm - Schreibweise mit "import"
import fs from 'node:fs'; // "fs" ist ein Modul aus der Node Standardbib und kann mit Prefix "node:" hervorgehoben werden
import color from 'chalk';
const getFileBy = (path, encoding = 'utf-8') => {
return new Promise((resolve, reject) => {
// async process
fs.readFile(path, encoding, (err, data) => {
if (err) {
// console.error(err);
reject(err);
}
// console.log(data);
resolve(data);
});
});
};
const getPromisesFileBy = (path, encoding = 'utf-8') => {
// Unterobjekt "promises" von fs besitzt Ummantelung von Promise-Objekt und Verarbeitung der Callback-Funktion
return fs.promises.readFile(path, encoding);
};
// Funktionsaufruf von Funktion mit async process
getFileBy('package.json')
.then((data) => {
console.log(color.yellow(data));
})
.catch((err) => {
console.log(color.red(err));
});
getPromisesFileBy('package.json')
.then((data) => {
console.log(color.green(data));
})
.catch((err) => {
console.log(color.magenta(err));
});

View File

@@ -0,0 +1,28 @@
{
"name": "module",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "module",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"chalk": "^5.6.2"
}
},
"node_modules/chalk": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
"license": "MIT",
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
}
}
}

View File

@@ -0,0 +1,16 @@
{
"name": "03_fs-promises",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"chalk": "^5.6.2"
}
}

View File

@@ -0,0 +1,35 @@
// esm - Schreibweise mit "import"
import fs from 'node:fs'; // "fs" ist ein Modul aus der Node Standardbib und kann mit Prefix "node:" hervorgehoben werden
import color from 'chalk';
// async ... await
const getFileBy = async (path, encoding = 'utf-8') => {
try {
const result = await fs.promises.readFile(path, encoding);
return result;
} catch (error) {
throw new Error(error);
}
};
const getPromiseFileBy = (path, encoding = 'utf-8') => {
return new Promise((resolve, reject) => {
fs.readFile(path, encoding, (err, data) => {
if (err) {
reject(err);
}
resolve(data);
});
});
};
getFileBy('package.json')
.then((data) => {
console.log(color.green(data));
})
.catch((err) => {
console.log(color.magenta(err));
})
.finally(() => {
console.log(color.yellow('process finished'));
});

View File

@@ -0,0 +1,28 @@
{
"name": "module",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "module",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"chalk": "^5.6.2"
}
},
"node_modules/chalk": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
"license": "MIT",
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
}
}
}

View File

@@ -0,0 +1,16 @@
{
"name": "04_async-await",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"chalk": "^5.6.2"
}
}

View File

@@ -0,0 +1 @@
Ich bin file01

View File

@@ -0,0 +1 @@
Ich bin file02

View File

@@ -0,0 +1 @@
Ich bin file03

View File

@@ -0,0 +1 @@
Ich bin file04

View File

@@ -0,0 +1,28 @@
import fs from 'node:fs';
import color from 'chalk';
const getFileBy = (path, encoding = 'utf-8') => {
return fs.promises.readFile(path, encoding);
};
// getFileBy('data/file01.txt')
// .then((data) => {
// console.log(color.green(data));
// })
// .catch((err) => {
// console.log(color.red(err));
// });
Promise.all([
getFileBy('data/file01.txt'),
getFileBy('data/file02.txt'),
// getFileBy('data/404'),
getFileBy('data/file03.txt'),
getFileBy('data/file04.txt'),
])
.then((data) => {
console.log(data);
})
.catch((err) => {
console.log(err);
});

View File

@@ -0,0 +1,28 @@
{
"name": "05_promise-all",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "05_promise-all",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"chalk": "^5.6.2"
}
},
"node_modules/chalk": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
"integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
"license": "MIT",
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
}
}
}

View File

@@ -0,0 +1,16 @@
{
"name": "05_promise-all",
"version": "1.0.0",
"description": "",
"keywords": [],
"license": "ISC",
"author": "",
"type": "module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"chalk": "^5.6.2"
}
}

View File

@@ -0,0 +1 @@
Ich bin eine Datei!

View File

@@ -0,0 +1,16 @@
// const fs = require('fs');
// const util = require('util');
import fs from 'node:fs';
import util from 'node:util';
// util.promisify fängt das letzte Argument als Callback-Funktion ab und ummantel die Funktion mit einem Promise-Objekt.
// Das ummantelte Promise-Objekt wird zusätzlich von einer weiteren Funktion mit den Argumenten ummantelt.
// https://javascript.info/promisify
// https://medium.com/trabe/understanding-nodes-promisify-and-callbackify-d2b04efde0e0
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,13 @@
{
"name": "01_util-promisify",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module"
}