This commit is contained in:
Philippe Torrel
2026-09-23 14:16:14 +02:00
parent 7f7d3806b2
commit 6eddd24eab
2 changed files with 10 additions and 5 deletions
@@ -1,11 +1,9 @@
// fs = FileSystem Modul aus der Node Standardbib (core)
// const fs = require('node:fs'); // commonjs Schreibweise
// const color = require('chalk');
import fs from 'node:fs'; // module - ESM Schreibweise
import color from 'chalk';
// Funktion mit einem asynchronen Prozess
const getContentBy = (path = '', encoding = 'utf-8') => {
const getFileBy = (path = '', encoding = 'utf-8') => {
return new Promise((resolve, reject) => {
// async process
fs.readFile(path, encoding, (error, data) => {
@@ -20,8 +18,15 @@ const getContentBy = (path = '', encoding = 'utf-8') => {
});
};
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);
};
// Funktionsaufruf einer Funktion mit asynchronem Prozess
getContentBy('./data/products.csv')
getPromisesFileBy('./data/products.csv')
.then((data) => {
console.log(color.yellow(data));
})
@@ -1,5 +1,5 @@
{
"name": "05_promise-readfile",
"name": "07_fs-promises",
"version": "1.0.0",
"description": "",
"main": "index.js",