This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user