This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
name, category, price
|
||||
Klingon Letter Opener, Office Warfare, 19.99
|
||||
Backpack of Holding, Travel, 29.99
|
||||
Tardis Alarmclock, Merchandise, 15.99
|
||||
|
@@ -0,0 +1,59 @@
|
||||
// fs = FileSystem Modul aus der Node Standardbib (core)
|
||||
import fs from 'node:fs'; // module - ESM Schreibweise
|
||||
import util from 'node:util';
|
||||
import color from 'chalk';
|
||||
|
||||
// Funktion mit einem asynchronen Prozess
|
||||
const getFileBy = (path = '', encoding = 'utf-8') => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// async process
|
||||
fs.readFile(path, encoding, (error, data) => {
|
||||
if (error) {
|
||||
// console.error(error);
|
||||
reject(error);
|
||||
}
|
||||
|
||||
// console.log(data);
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const getPromisesFileBy = (path = '', encoding = 'utf-8') => {
|
||||
// Unterobjekt "promises" von fs besitzt Ummantelung von Promise-Objekt und die Verarbeitung der Callback-Funktion
|
||||
|
||||
// async process
|
||||
return fs.promises.readFile(path, encoding);
|
||||
};
|
||||
|
||||
// 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 getPromisifyFileBy = util.promisify(fs.readFile);
|
||||
|
||||
// Funktionsaufruf einer Funktion mit asynchronem Prozess
|
||||
getFileBy('./data/products.csv')
|
||||
.then((data) => {
|
||||
console.log(color.yellow(data));
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(color.red(err));
|
||||
});
|
||||
|
||||
getPromisesFileBy('./data/products.csv')
|
||||
.then((data) => {
|
||||
console.log(color.yellow(data));
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(color.red(err));
|
||||
});
|
||||
|
||||
getPromisifyFileBy('./data/products.csv', 'utf-8')
|
||||
.then((data) => {
|
||||
console.log(color.yellow(data));
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(color.red(err));
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "05_promise-readfile",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "05_promise-readfile",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"chalk": "^6.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/chalk": {
|
||||
"version": "6.0.0",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-6.0.0.tgz",
|
||||
"integrity": "sha512-2uNTXIuTTxk7ciZgAU1BQcgnchcG0xXnrs6jzkQfj9SsRa9M2s5zE8WT96hS6KmG4MzWHSrvH43DF1m4XRkrFg==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=22"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "07_fs-promises",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"chalk": "^6.0.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user