Jieunny์˜ ๋ธ”๋กœ๊ทธ

S2) Unit3. [์‹ค์Šต] fs ๋ชจ๋“ˆ ๋ณธ๋ฌธ

CodeStates/Training

S2) Unit3. [์‹ค์Šต] fs ๋ชจ๋“ˆ

Jieunny 2023. 1. 19. 17:00

๐Ÿ“ฃ fs ๋ชจ๋“ˆ์„ ํ†ตํ•ด ๋น„๋™๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ

โœ”๏ธ fs.readFile("filename", [options], callback)

โžฐ "filename" ํŒŒ์ผ์„ [options] ์„ ์ ์šฉํ•ด ์ฝ์€ ํ›„ callback ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

โžฐ ํŒŒ์ผ์„ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

โžฐ ๋‘๋ฒˆ์งธ ์ธ์ž๋ฅผ "utf8" ๋กœ ๋ช…์‹œํ•˜์—ฌ ์ธ์ฝ”๋”ฉ์ด ๋˜๋„๋ก ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

 

1๏ธโƒฃ callback.js

const fs = require("fs");

const getDataFromFile = function (filePath, callback) {
  // TODO: fs.readFile์„ ์ด์šฉํ•ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค
  fs.readFile(filePath, 'utf8', (err, data) => {
    if(err){
      callback(err, null);
    }
    else{
      callback(null, data);
    }
  });
}

getDataFromFile("README.md", (err, data) => {
// ๋ฐ‘์˜ ๋‚ด์šฉ์ด ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
  console.log(data);
});

module.exports = {
  getDataFromFile
};

โžฐ fs.readFile() ์€ ํŒŒ์ผ์„ ์ฝ๋Š” ๊ณผ์ •์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋ฉด ์ต๋ช…์˜ ํ•จ์ˆ˜ err์„ ๋˜์ง€๊ณ , ์„ฑ๊ณตํ•˜๋ฉด data์— ๊ทธ ๋‚ด์šฉ์„ ๋‹ด๋Š”๋‹ค.

โžฐ err๊ฐ€ ๋‚ ์•„์˜ค๋ฉด callback ํ•จ์ˆ˜์— err๋ฅผ ์ธ์ž๋กœ ์ฃผ๊ณ , ์„ฑ๊ณตํ•˜๋ฉด callback ํ•จ์ˆ˜์— data๋ฅผ ์ธ์ž๋กœ ์ค€๋‹ค.

 

 

2๏ธโƒฃ promiseConstructor.js

const fs = require("fs");

const getDataFromFilePromise = filePath => {
  // return new Promise()
  // TODO: Promise ๋ฐ fs.readFile์„ ์ด์šฉํ•ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
  return new Promise((resolve, reject) => {
    fs.readFile(filePath, "utf8", (err, data) => {
      if(err){
        reject(err);
      }
      else{
        resolve(data);
      }
    });
  })
};

getDataFromFilePromise('README.md').then(data => console.log(data));

module.exports = {
  getDataFromFilePromise
};

โžฐ Promise๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์œ„์™€ ๋˜‘๊ฐ™์ด ์ž‘๋™ํ•˜๋Š” ์ฝ”๋“œ ์ž‘์„ฑ

โžฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด reject๋ฅผ, ์„ฑ๊ณตํ•˜๋ฉด resolve๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

 

 

3๏ธโƒฃ basicChaining.js

const path = require('path');
const { getDataFromFilePromise } = require('./02_promiseConstructor');

const user1Path = path.join(__dirname, 'files/user1.json');
const user2Path = path.join(__dirname, 'files/user2.json');

// HINT: getDataFromFilePromise(user1Path) ๋ฐ getDataFromFilePromise(user2Path)๋ฅผ ์ด์šฉํ•ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค
const readAllUsersChaining = () => {
  // TODO: ์—ฌ๋Ÿฌ๊ฐœ์˜ Promise๋ฅผ then์œผ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค
  let result = [];

  return getDataFromFilePromise(user1Path)
    .then((user1) => {
        result.push(JSON.parse(user1));
        return getDataFromFilePromise(user2Path)
          .then((user2) => {
            result.push(JSON.parse(user2));
            return result;
          })
    })
}

// readAllUsersChaining();

module.exports = {
  readAllUsersChaining
}

โžฐ ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๋Š” ํ•จ์ˆ˜๋Š” fs.readFile์ด ์•„๋‹Œ callback.js์—์„œ ๊ตฌํ˜„ํ•œ getDataFromFilePromise๋ฅผ ์ด์šฉํ•œ๋‹ค.

โžฐ .then์„ ์‚ฌ์šฉํ•ด์„œ promise chain์„ ๊ตฌํ˜„ํ•œ๋‹ค.

โžฐ user1 ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ์–ด์˜ค๋Š”๋ฐ ์„ฑ๊ณตํ•˜๋ฉด, ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ user1 ์ด๋ผ๊ณ  ํ•˜๊ณ  ํŒŒ์‹ฑ ํ›„ result ๋ฐฐ์—ด์— ๋„ฃ์–ด์ค€๋‹ค.

โžฐ ์œ„ ๊ณผ์ •์ด ๋ชจ๋‘ ์„ฑ๊ณตํ•˜๋ฉด user2 ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ฝ์–ด์˜ค๊ณ  ๊ฐ™์€ ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค.

 

 

4๏ธโƒฃ promiseAll.js

const path = require('path');
const { getDataFromFilePromise } = require('./02_promiseConstructor');

const user1Path = path.join(__dirname, 'files/user1.json');
const user2Path = path.join(__dirname, 'files/user2.json');

const readAllUsers = () => {
  // TODO: Promise.all์„ ์ด์šฉํ•ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค
  let result = [];

  const user1 = getDataFromFilePromise(user1Path).then((data) => JSON.parse(data));
  const user2 = getDataFromFilePromise(user2Path).then((data) => JSON.parse(data));

  return Promise.all([user1, user2]).then((values) => {
    return values;
  });
}

// readAllUsers()

module.exports = {
  readAllUsers
}

โžฐ promise.all ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœํšŒํ•œ๋‹ค.

โžฐ user1๊ณผ user2์— ๊ฐ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€์„œ ์„ฑ๊ณตํ•˜๋ฉด, ํŒŒ์‹ฑ ํ›„ ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

โžฐ user1๊ณผ user2๋ฅผ promise.all์˜ ์ธ์ž๋กœ ์ฃผ์–ด์„œ ์ˆœํšŒํ•˜๊ฒŒ ํ•˜๊ณ , ๊ทธ ๋ฐ์ดํ„ฐ๋“ค์€ values์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

 

 

5๏ธโƒฃ asyncAwait.js

const path = require('path');
const { getDataFromFilePromise } = require('./02_promiseConstructor');

const user1Path = path.join(__dirname, 'files/user1.json');
const user2Path = path.join(__dirname, 'files/user2.json');

const readAllUsersAsyncAwait = async() => {
  // TODO: async/await ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค
  let result = [];
  const user1 = await getDataFromFilePromise(user1Path).then((data)=> JSON.parse(data));
  const user2 = await getDataFromFilePromise(user2Path).then((data)=> JSON.parse(data));

  result = [user1, user2];
  return result;
}

readAllUsersAsyncAwait();

module.exports = {
  readAllUsersAsyncAwait
}

โžฐ readAllUsersAsyncAwait์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” async()๊ฐ€ ๋ถ™์—ˆ์œผ๋ฏ€๋กœ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

โžฐ const user1์— await๋ฅผ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— user1 ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ์–ป์–ด์•ผ ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. -> ๋น„๋™๊ธฐ์ 

 

 

'CodeStates > Training' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

S2) Unit 6. [์‹ค์Šต] React Twittler State & Props  (0) 2023.01.27
S2) Unit3. [์‹ค์Šต] fetch API  (0) 2023.01.19
S2) Unit3. [์‹ค์Šต] ํƒ€์ด๋จธ API  (0) 2023.01.18
S2) Unit3. [์‹ค์Šต] Underbar  (0) 2023.01.17
S2) Unit2. [์‹ค์Šต] Beesbeesbees  (0) 2023.01.16