Skip to content

Conversation

@tars-mj
Copy link

@tars-mj tars-mj commented Jan 30, 2018

No description provided.

Copy link
Contributor

@Ami777 Ami777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super rozwiązanie.

let sum = JSON.parse(data).reduce((x, y) => x + y);

fs.writeFile('./data/zadanie01/sum.txt', sum, err => {
let msg = err ? 'Błąd zapisu pliku' : 'Plik poprawnie zapisany';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Dodatkowo, można było użyć const.


files.forEach(file => {
console.log(`Nazwa pliku: ${file}`);
let data = fs.readFileSync(`${path}${file}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Staramy się zawsze używać funkcji asynchronicznych - dlatego tylko takie poznaliśmy. Dzięki temu kiedy Node.js "nudzi się" odczytem pliku - mogą się dziać inne rzeczy.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tak racja. Chciałem tym uzyskać odczyt pliku pod nazwą pliku bez rejestrowania eventu :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jasna sprawa :) Wygląda "prościej", ale właśnie nie korzysta z tej przewagi Node.
Natomiast taka ciekawostka, że używając Promise + async/await da się pisać kod który wygląda jak synchroniczny, a jest asynchroniczny ^^

fs.readFile(path, 'utf-8', (err, data) => {
if (err) return console.log('Błąd odczytu pliku');

let newData = [...data].map((elem, i) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Najlepszy sposób.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dziękuję :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants