Questions regarding fs in node.js

purpose

Know when they are executing fs.writeFileSync()or fs.writeFile()recording a file, so I can perform another function.

Background

I am writing a file using fs, and to find out when the file will be written, I checked its documentation:

After reading, I focused on both fs.writeFile(), and on fs.writeFileSync(), but I do not see the missing documentation, and I have doubts.

Questions about fs.writeFile()

  • Is the callback function called before the file is written, how it is written, or after it has been written and saved to disk?

Questions about fs.writeFileSync()

  • How do I know if an error has occurred if it always returns undefined?
  • , , , ?

  • , , , fs.writeFile()?
  • NPM ? , .

fs, , . , , .


!

+4
5

, . , , GitHub. , fs.writeFileSync() Github. . , .

( GitHub 10x , ). . , , .

...

fs.writeFile()

, , , ?

, ( ).

fs.writeFileSync()

  • , , undefined?

fs.writeFileSync , . try/catch, .

  1. , , , ?

, , . , node.js.

, , fs.writeFile()?

promises Promise.all() .

Bluebird:

const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));

Promise.all([fs.writeFileAsync(fname1, data1), fs.writeFileAsync(fname2, data2)]).then(function() {
    // both async operations are done here
}).catch(function(err) {
    // handle error here
});

Bluebird .promisifyAll() async fs , , Async, fs.writeFile() fs.writeFileAsync(), . Promise, Promise.all() .

Bluebird, promises node.js( ES6), node.js async Bluebird. , .

const fs = require('fs');

fs.writeFileAsync = function(fname, data) {
    return new Promise(function(resolve, reject) {
        fs.writeFile(fname, data, function(err) {
            if (err) return reject(err);
            resolve();
        });
    });
}

Promise.all([fs.writeFileAsync(fname1, data1), fs.writeFileAsync(fname2, data2)]).then(function() {
    // both async operations are done here
}).catch(function(err) {
    // handle error here
});
+3

... :

(fs.writeFile()...) , , , ?

, .

(fs.writeFileSync()...) , , undefined?

.

, , , ?

! .


, , , fs.writeFile()?

:

const fs = require('fs');

let completions = 0;

function onComplete(err) {
  if(err) {
    console.log('An error occurred', err);
  }

  completions++;
  if(completions === 2) {
    console.log('All done!');
  }
}

fs.writeFile('file1.txt', 'Hello', onComplete);
fs.writeFile('file2.txt', 'World', onComplete);

NPM ? , .

( ...), promises fs . Promise.all, !

+1

fs.writeFile() , , , ?

, .

, fs.writeFileSync(), undefined?

> fs.writeFileSync('/test', 'foo')
Error: EACCES: permission denied, open '/test'
    at Error (native)
    ...

throw , try..catch.

fs.writeFileSync() , , , ?

; , , .

, , , fs.writeFile()?

promises:

let write1 = new Promise((resolve, reject) => {
    fs.writeFile(file1, data1, err => {
        if (err) {
            reject(err);
        } else {
            resolve();
        }
    });
});

let write2 = new Promise(/* same thing here */);

Promise.all([write1, write2]).then(() => {
    console.log('both writes done');
});
+1

fs.writeFile() . , . fs.writeFileSync() , , . , -, , aysnc,

fs.writeFile('test.txt', 'text', function(){
    fs.writeFile('test2.txt', 'text', function(){
        console.log('done');
    });
});

, , .

0

:

Q1) , , , ?

Ans1) . , . , , . , , callback " ".

Q2) , , undefined?

Ans2) try catch, , , . .

Q3) , , , ?

Ans3) , , / , .

Q4) , , , fs.writeFile()?

Ans4) , . , . , . . .

fs.writeFile('file.txt', 'hello', function(error){ //do something }); fs.writeFile('file1.txt', 'Amulya', function(error){ //do something});

they will be executed at the same time because they are asynchronous in nature and they will be without any problems.

Q5) Is there a convenient library in NPM to do this kind of work? I searched, but I did not find anything convincing.

Ans5). There is no need for an NPM library if the bopth task is independent of each other, as in the example above. If they depend on each other, you can use asyncor bluebirdin the chain, or to promise them.

0
source

Source: https://habr.com/ru/post/1656770/


All Articles