LoginSignup
0
0

More than 5 years have passed since last update.

Nodeでファイル書き込み

Posted at

ファイル書き込み(新規作成/追記)

fs.appendFile

const fs = require('fs');
const textFile = 'test.txt';

// return Promise
exports.write = (text) => {

  return (async () => {

    await new Promise((resolve, reject) => {
      fs.appendFile(textFile, textFile, (error) => {
        error ? reject(error) : resolve();
      });
    }).catch((error) => {
      console.log(error);
    });

  })();
}

とやるのかと思ってたけど同期処理があった。

fs.appendFileSync

const fs = require('fs');
const textFile = 'test.txt';

// return Promise
exports.write = (text) => {
  try {
    fs.appendFileSync(textFile, text);
  } catch (error) {
    console.log(error);
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0