cozimo
cozimo

Reputation: 115

NodeJS: Asynchronous Function Without Callback: Need two callbacks?

I don't understand why this has an deprecation warning. I have looked at the other Q&A and I still don't get it. Does deprecation mean that it's just old school and no longer best practice? What should I be doing? Thanks a bunch!

let fs = require('fs');

fs.mkdir('stuff', ()=>{
  fs.readFile('readMe.txt', (err, data)=>{
    if(err){
      throw err;
    };
    fs.writeFile('./stuff/writeMe.txt', (err, data)=>{
      if (err){
        throw err;
      };
    });
  });
});

Upvotes: 0

Views: 239

Answers (1)

desoares
desoares

Reputation: 861

The deprecation message is shown because your fs.writeFile is missing one argument, the data to write on the file:

let fs = require('fs');

fs.mkdir('stuff', () => {
  fs.readFile('readMe.txt', (err, data)=>{
    if (err) {
      throw err;
    }
    console.log(data.toString());
    fs.writeFile('./stuff/writeMe.txt', 'test string to write', (err, data) => {
      if (err) {
        throw err;
      }

      console.log(data);
    });
  });
});

Without the data to write, witch is not optional, there is no callback argument.

Upvotes: 1

Related Questions