Notice
Recent Posts
Recent Comments
Link
ยซ   2024/05   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

[Node.js] #2 ์•Œ์•„๋‘ฌ์•ผ ํ•  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ณธ๋ฌธ

๐Ÿ›ฐ๏ธ Node.js

[Node.js] #2 ์•Œ์•„๋‘ฌ์•ผ ํ•  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2023. 7. 17. 19:47

#2 ์•Œ์•„๋‘ฌ์•ผ ํ•  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

 

ES2015(ES6) ์ดํ›„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค.

 

 

๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•

if (true) {
  var x = 3;
}
console.log(x); // 3

if (true) {
  const y = 3;
}
console.log(y); // Uncaught ReferenceError: y is not defined

 

var๋Š” ํ•จ์ˆ˜ ์Šค์ฝ”ํ”„๋ฅผ ๊ฐ€์ ธ์„œ if๋ฌธ์˜ ๋ธ”๋ก๊ณผ ๊ด€๊ณ„์—†์ด ์ ‘๊ทผ ๊ฐ€๋Šฅ.

const์™€ let์€ ๋ธ”๋ก ์Šค์ฝ”ํ”„๋ฅผ ๊ฐ€์ ธ์„œ ๋ธ”๋ก ๋ฐ–์—์„œ๋Š” ๋ณ€์ˆ˜์— ์ ‘๊ทผ ๋ถˆ๊ฐ€.

 

const a = 0;
a = 1; // Uncaught TypeError: Assignment to constant variable.
let b = 0;
b = 1; // 1
const c; // Uncaught SyntaxError: Missing initializer in const declaration

const๋Š” ์ดˆ๊ธฐ๊ฐ’ ์ฃผ์–ด์•ผํ•˜๋ฉฐ ๊ฐ’ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€. (swift๋กœ ์น˜๋ฉด static let)

let์€ ๊ฐ’ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ

 

 

# ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด

์•„๋ž˜ ์˜ˆ์‹œ๊ฐ€ ์žˆ๋Š”๋ฐ ๋ฐฑํ‹ฑ์œผ๋กœ ์ŠคํŠธ๋ง์„ ๋ฌถ๋Š”๊ฑฐ ์™ธ์—๋Š” ํฌ๊ฒŒ swift๋ž‘ ๋‹ค๋ฅธ๊ฑด ์•ˆ๋ณด์ž„

// ๊ธฐ์กด ES5 ๋ฌธ๋ฒ•
var num1 = 1;
var num2 = 2;
var result = 3;
var string1 = num1 + ' ๋”ํ•˜๊ธฐ ' + num2 + '๋Š” \'' + result + '\'';
console.log(string1); // 1 ๋”ํ•˜๊ธฐ 2๋Š” '3'

// ES2015(ES6) ๋ฌธ๋ฒ•
const num3 = 1;
const num4 = 2;
const result2 = 3;
const string2 = `${num3} ๋”ํ•˜๊ธฐ ${num4}๋Š” '${result2}'`;
console.log(string2); // 1 ๋”ํ•˜๊ธฐ 2๋Š” '3'

 

# ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด

 

sayJs ๊ฐ™์€ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ์— ํ•จ์ˆ˜๋ฅผ ์—ฐ๊ฒฐํ•  ๋•Œ ๋”๋Š” ์ฝœ๋ก ๊ณผ function์„ ๋ถ™์ด์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

// ๊ธฐ์กด
var sayNode = function() {
  console.log('Node');
};
var es = 'ES';
var oldObject = {
  sayJS: function() {
    console.log('JS');
  },
  sayNode: sayNode,
};
oldObject[es + 6] = 'Fantastic';
oldObject.sayNode(); // Node
oldObject.sayJS(); // JS
console.log(oldObject.ES6); // Fantastic

// ์ƒˆ๋กญ๊ฒŒ ๋“ฑ์žฅ
const newObject = {
  sayJS() {
    console.log('JS');
  },
  sayNode,
  [es + 6]: 'Fantastic',
};
newObject.sayNode(); // Node
newObject.sayJS(); // JS
console.log(newObject.ES6); // Fantastic

 

# ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ 

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—์„œ๋Š” function๋Œ€์‹  => ๊ธฐํ˜ธ๋กœ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธ.

ํ™”์‚ดํ‘œํ•จ์ˆ˜์—์„œ๋Š” ๋‚ด๋ถ€์— return๋ฐ–์— ์—†๋Š” ๊ฒฝ์šฐ ์ฝ”๋“œ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Œ.

// add1, 2, 3, 4๋Š” ๋ชจ๋‘ ๊ฐ™์€ ๊ธฐ๋Šฅ
function add1(x, y) {
  return x + y;
}

const add2 = (x, y) => {
  return x + y;
};

const add3 = (x, y) => x + y;

const add4 = (x, y) => (x + y);

// not๋„ ๊ฐ™์€ ๊ธฐ๋Šฅ
function not1(x) {
  return !x;
}

const not2 = x => !x;

 

๊ธฐ์กด functionrhk ๋‹ค๋ฅธ this์˜ ๋ฐ”์ธ๋”ฉ ๋ฐ•์‹์„ ๋ณด์ž.

 - this๋Š” ์ž๋ฐ”์—์„œ ๋งŽ์ด ๋ณด์•˜๊ณ , ์Šค์œ„ํ”„ํŠธ์˜ self๋ž‘ ๋‹ฎ์•˜์œผ๋ฉฐ forEach์— => ๋Š” in์œผ๋กœ ๋Š๊ปด์ง€๊ณ  { } ๋Š” ์žˆ์–ด์•ผ ํ•˜๋‚˜ ์Šค์œ„ํ”„ํŠธ๋ž‘ ๋‹ฌ๋ฆฌ ๊ฐ’์„ ๋ฐ–์—์„œ ๊บผ๋‚ด์„œ ์ •์˜ํ•˜๋Š”๋“ฏ.

 

var relationship1 = {
  name: 'zero',
  friends: ['nero', 'hero', 'xero'],
  logFriends: function () {
    var that = this; // relationship1์„ ๊ฐ€๋ฆฌํ‚ค๋Š” this๋ฅผ that์— ์ €์žฅ
    this.friends.forEach(function (friend) {
      console.log(that.name, friend);
    });
  },
};
relationship1.logFriends();

const relationship2 = {
  name: 'zero',
  friends: ['nero', 'hero', 'xero'],
  logFriends() {
    this.friends.forEach(friend => {
      console.log(this.name, friend);
    });
  },
};
relationship2.logFriends();

 

 

 

# ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

๊ฐ์ฒด์™€ ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ์†์„ฑ์ด๋‚˜ ์š”์†Œ๋ฅผ ์‰ฝ๊ฒŒ ๊บผ๋‚ผ ์ˆ˜ ์žˆ์Œ.

๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•จ์ˆ˜์˜ this๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Œ. ๋‹ฌ๋ผ์ง„ this๋ฅผ ์›๋ž˜๋Œ€๋กœ ๋˜๋Œ๋ ค์ฃผ๋ ค๋ฉด getCandyํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

var candyMachine = {
  status: {
    name: 'node',
    count: 5,
  },
  getCandy: function () {
    this.status.count--;
    return this.status.count;
  },
};
var getCandy = candyMachine.getCandy;
var count = candyMachine.status.count;

// ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ๋„ ์ž‘์„ฑ ๊ฐ€๋Šฅ.
const candyMachine = {
  status: {
    name: 'node',
    count: 5,
  },
  getCandy() {
    this.status.count--;
    return this.status.count;
  },
};
const { getCandy, status: { count } } = candyMachine;

 

๋ฐฐ์—ด์— ๋Œ€ํ•œ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๋ฐฉ๋ฒ•๋„ ์กด์žฌํ•˜๋Š”๋ฐ,

var array = ['nodejs', {}, 10, true];
var node = array[0];
var obj = array[1];
var bool = array[3];

const array = ['nodejs', {}, 10, true];
const [node, obj, , bool] = array;

 

# ํด๋ž˜์Šค

๋‹ค๋ฅธ ์–ธ์–ด์ฒ˜๋Ÿผ ํด๋ž˜์Šค ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์—ฌ์ „ํžˆ ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘.

 

// ๊ธฐ์กด์˜ ํ”„๋กœํ† ํƒ€์ž… ์ƒ์† ์˜ˆ์ œ ์ฝ”๋“œ
var Human = function(type) {
  this.type = type || 'human';
};

Human.isHuman = function(human) {
  return human instanceof Human;
}

Human.prototype.breathe = function() {
  alert('h-a-a-a-m');
};

var Zero = function(type, firstName, lastName) {
  Human.apply(this, arguments);
  this.firstName = firstName;
  this.lastName = lastName;
};

Zero.prototype = Object.create(Human.prototype);
Zero.prototype.constructor = Zero; // ์ƒ์†ํ•˜๋Š” ๋ถ€๋ถ„
Zero.prototype.sayName = function() {
  alert(this.firstName + ' ' + this.lastName);
};
var oldZero = new Zero('human', 'Zero', 'Cho');
Human.isHuman(oldZero); // true

// ์•„๋ž˜๋Š” ํด๋ž˜์Šค ๊ธฐ๋ฐ˜

class Human {
  constructor(type = 'human') {
    this.type = type;
  }

  static isHuman(human) {
    return human instanceof Human;
  }

  breathe() {
    alert('h-a-a-a-m');
  }
}

class Zero extends Human {
  constructor(type, firstName, lastName) {
    super(type);
    this.firstName = firstName;
    this.lastName = lastName;
  }

  sayName() {
    super.breathe();
    alert(`${this.firstName} ${this.lastName}`);
  }
}

const newZero = new Zero('human', 'Zero', 'Cho');
Human.isHuman(newZero); // true

ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜ ์˜ˆ์ œ๋Š” ์ž˜ ์™€๋‹ฟ์ง€ ์•Š์Œ. ํ•˜์ง€๋งŒ ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ ์ฝ”๋“œ๋Š” constructor๊ฐ€ ์Šค์œ„ํ”„ํŠธ์˜ init + ๋ณ€์ˆ˜์„ ์–ธ์„ ๋‹ด๋‹นํ•˜๊ณ  sayName์„ func ๊ทธ๋ฆฌ๊ณ  super๋ฅผ ํ†ตํ•ด ์ƒ์†๋ฐ›์€ ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉฐ $ { } ์—์„œ๋Š” constructor์—์„œ ์ •์˜ํ•œ ๊ฑธ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™์Œ.

 

ํƒ€์ž…์„ string์œผ๋กœ ์ •์˜ํ•˜๋Š”๊ฒŒ ์ข€ ํŠน์ดํ•ด๋ณด์ž„.

 

# ํ”„๋กœ๋ฏธ์Šค

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ๋…ธ๋“œ์˜ API๋“ค์ด ์ฝœ๋ฐฑ ๋Œ€์‹œ ํ”„๋กœ๋ฏธ์Šค ๊ธฐ๋ฐ˜์œผ๋กœ ์žฌ๊ตฌ์„ฑ.

์•…๋ช… ๋†’์€ ์ฝœ๋ฐฑ์ง€์˜ฅ ํ˜„์ƒ ๊ทน๋ณตํ–ˆ๋‹ค๋Š” ํ‰๊ฐ€.

 

๋‹ค๋งŒ ํ”„๋กœ๋ฏธ์Šค๋Š” ๊ทœ์น™์ด ์žˆ์Œ. ๋จผ์ € ํ”„๋กœ๋ฏธ์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•จ.

const condition = true; // true๋ฉด resolve, false๋ฉด reject
const promise = new Promise((resolve, reject) => {
  if (condition) {
    resolve('์„ฑ๊ณต');
  } else {
    reject('์‹คํŒจ');
  }
});
// ๋‹ค๋ฅธ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Œ
promise
  .then((message) => {
    console.log(message); // ์„ฑ๊ณต(resolve)ํ•œ ๊ฒฝ์šฐ ์‹คํ–‰
  })
  .catch((error) => {
    console.error(error); // ์‹คํŒจ(reject)ํ•œ ๊ฒฝ์šฐ ์‹คํ–‰
  })
  .finally(() => { // ๋๋‚˜๊ณ  ๋ฌด์กฐ๊ฑด ์‹คํ–‰
    console.log('๋ฌด์กฐ๊ฑด');
  });

 

ํ”„๋กœ๋ฏธ์Šค์˜ ๋‚ด๋ถ€์—์„œ resolve๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด then์œผ๋กœ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด catch๋กœ

finally๋Š” ๋ฌด์กฐ๊ฑด ์‹คํ–‰.

 

new Promise๋Š” ๋ฐ”๋กœ ์‹คํ–‰๋˜๋‚˜ ๊ฒฐ๊ด๊ฐ’์€ then์„ ๋ถ™์˜€์„ ๊ฒฝ์šฐ์—๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ.

 

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœํ•˜๋Š” ์˜ˆ์ œ

promise 
  .then((message) => {
    return new Promise((resolve, reject) => {
      resolve(message);
    });
  })
  .then((message2) => {
    console.log(message2);
    return new Promise((resolve, reject) => {
      resolve(message2);
    });
  })
  .then((message3) => {
    console.log(message3);
  })

  .catch((error) => {
    console.error(error);
  });
  
  // ์‹คํ–‰ ์ •๋ฆฌ
  ์ฒ˜์Œ then์—์„œ resolve๋ฅผ ํ•˜๋ฉด ๋‹ค์Œ then์—์„œ message2๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ.
  ๋‹ค์‹œ message2๊ฐ€ resolveํ•œ ๊ฒƒ์„ message3๋กœ ๋ฐ›์Œ.
  ๋‹จ, then์—์„œ new Promise๋ฅผ returnํ•ด์•ผ๋งŒ ๋‹ค์Œ then์—์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ.

 

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์ฝœ๋ฐฑ์„ ์“ฐ๋Š” ํŒจํ„ด์— ๋Œ€ํ•œ ์ •๋ฆฌ

๊ฐ€์žฅ ๋ˆˆ์— ๋„๋Š” ์ฐจ์ด๋Š” ์—๋Ÿฌ๋ฅผ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ์•„๋‹ˆ๋ฉด ๊ฐ๊ฐ ๋Œ€์‘ํ•ด์ฃผ๋Š”์ง€ ์—ฌ๋ถ€.

// ์ฝœ๋ฐฑํ•จ์ˆ˜ 3์ค‘์ฒฉ. -> ๊ฐ ์ฝœ๋ฐฑ๋งˆ๋‹ค ์—๋Ÿฌ๋„ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.
function findAndSaveUser(Users) {
  Users.findOne({}, (err, user) => { // ์ฒซ ๋ฒˆ์งธ ์ฝœ๋ฐฑ
    if (err) {
      return console.error(err);
    }
    user.name = 'zero';
    user.save((err) => { // ๋‘ ๋ฒˆ์งธ ์ฝœ๋ฐฑ
      if (err) {
        return console.error(err);
      }
      Users.findOne({ gender: 'm' }, (err, user) => { // ์„ธ ๋ฒˆ์งธ ์ฝœ๋ฐฑ
        // ์ƒ๋žต
      });
    });
  });
}

// ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณ€ํ˜•

function findAndSaveUser(Users) {
  Users.findOne({})
    .then((user) => {
      user.name = 'zero';
      return user.save();
    })
    .then((user) => {
      return Users.findOne({ gender: 'm' });
    })
    .then((user) => {
      // ์ƒ๋žต
    })
    .catch(err => {
      console.error(err);
    });
}

 

 

์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” ์ฝ”๋“œ์˜ ๊นŠ์ด๊ฐ€ ๋”์ด์ƒ ๊นŠ์–ด์ง€์ง€ ์•Š์Œ. ๊ฐ๊ฐ error๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ catch ํ•œ๋ฒˆ์œผ๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ.

 

ํ”„๋กœ๋ฏธ์Šค ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ํ•œ๋ฒˆ์— ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•๋„ ์กด์žฌ. (Pomise.all)

const promise1 = Promise.resolve('์„ฑ๊ณต1');
const promise2 = Promise.resolve('์„ฑ๊ณต2');
Promise.all([promise1, promise2])
  .then((result) => {
    console.log(result); // ['์„ฑ๊ณต1', '์„ฑ๊ณต2'];
  })
  .catch((error) => {
    console.error(error);
  });

๊ทผ๋ฐ all์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฐฐ์—ด์— ๋“ค์–ด๋Š” ๊ฒƒ์ค‘ ํ•˜๋‚˜๋งŒ reject์—ฌ๋„ catch๋กœ ๋„˜์–ด๊ฐ€์„œ ์–ด๋–ค ์นœ๊ตฌ๊ฐ€ reject๋˜์—ˆ๋Š”์ง€ ๋ชจ๋ฆ„.

 

allSettled๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

const promise1 = Promise.resolve('์„ฑ๊ณต1');
const promise2 = Promise.resolve('์‹คํŒจ2');
const promise3 = Promise.resolve('์„ฑ๊ณต3');
Promise.allSettled([promise1, promise2, promise3])
  .then((result) => {
    console.log(result); 
    /*
    [
       { status: 'fulfilled', value: '์„ฑ๊ณต1' },
       { status: 'rejected', reason: '์‹คํŒจ2' },
       { status: 'fulfilled', value: '์„ฑ๊ณต3' }
    ]
    */
  })
  .catch((error) => {
    console.error(error);
  });

์–ด๋–ค ํ”„๋กœ๋ฏธ์Šค๊ฐ€ reject๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ.

 

์ฐธ๊ณ ๋กœ Node 16๋ถ€ํ„ฐ๋Š” reject๋œ promise์— catch๋ฅผ ๋‹ฌ์ง€ ์•Š์œผ๋ฉด UnhandledPromiseRejection ์—๋Ÿฌ ๋ฐœ์ƒ.

์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ, ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์Œ.

 

 

# async/await

 

Promise๊ฐ€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝœ๋ฐฑ์ง€์˜ฅ ํ•ด๊ฒฐํ–ˆ์–ด๋„ ์—ฌ์ „ํžˆ ์ฝ”๋“œ๊ฐ€ ์žฅํ™ฉํ•จ.

ES2017๋ถ€ํ„ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.

// ์ด ์ฝ”๋“œ๋ฅผ async/awiat๋ฅผ ํ†ตํ•ด ๊ต์ฒดํ•ด๋ณผ ์˜ˆ์ •
function findAndSaveUser(Users) {
  Users.findOne({})
    .then((user) => {
      user.name = 'zero';
      return user.save();
    })
    .then((user) => {
      return Users.findOne({ gender: 'm' });
    })
    .then((user) => {
      // ์ƒ๋žต
    })
    .catch(err => {
      console.error(err);
    });
}

// async/await ์˜ˆ์ œ
async function findAndSvaeUser(Users) { 
	let user = await Users.findOne({});
    user.name = 'zero';
    user = await Users.save();
    user = await Users.findOne({ gender: 'm' });
    // ์ƒ๋žต
}

์™€ ์ด๊ฑธ ๋ณด๋‹ˆ๊นŒ Swift์—์„œ async/await ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ดํ•ด๊ฐ€ ๋” ๊นŠ์–ด์ง€๋Š” ๊ธฐ๋ถ„์ด๋‹ค.

 

๋‹ค๋งŒ ์œ„ ์ฝ”๋“œ๋Š” ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„(reject ๊ฒฝ์šฐ)์ด ์—†์œผ๋ฏ€๋กœ ์ถ”๊ฐ€์ ์ธ ์ž‘์—…์ด ํ•„์š”. - ์•„๋ž˜์ฝ”๋“œ

async function findAndSaveUser(Users) {
  try {
    let user = await Users.findOne({});
    user.name = 'zero';
    user = await user.save();
    user = await Users.findOne({ gender: 'm' });
    // ์ƒ๋žต
  } catch (error) {
    console.error(error);
  }
}

์–ด๋–ค ํ”„๋กœ๋ฏธ์Šค๊ฐ€ reject๋œ ๊ฒฝ์šฐ.

 

์•„๋ž˜๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋„ async์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ.

const findAndSaveUser = async (Users) => {
  try {
    let user = await Users.findOne({});
    user.name = 'zero';
    user = await user.save();
    user = await Users.findOne({ gender: 'm' });
    // ์ƒ๋žต
  } catch (error) {
    console.error(error);
  }
};

 

for๋ฌธ๊ณผ async/await์„ ํ†ตํ•ด์„œ ํ”„๋กœ๋ฏธ์Šค ํ•˜๋‚˜์”ฉ ์ˆœ์ฐจ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ

ES2018๋ถ€ํ„ฐ ๊ฐ€๋Šฅ

const promise1 = Promise.resolve('์„ฑ๊ณต1');
const promise2 = Promise.resolve('์„ฑ๊ณต2');
(async () => {
  for await (promise of [promise1, promise2]) {
    console.log(promise);
  }
})();

 

๋ญ”๊ฐ€ ์Šค์œ„ํ”„ํŠธ๊ฐ€ ์ž์Šค๋ฅผ ๋งŽ์ด ๋”ฐ๋ผ๊ฐ„๋‹ค๋Š” ๊ธฐ๋ถ„์ด๋‹ค.

 

async ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์€ ํ•ญ์ƒ Promise๋กœ ๊ฐ์‹ธ์ง„๋‹ค. ๋”ฐ๋ผ์„œ ์‹คํ–‰ ํ›„ then์„ ๋ถ™์ด๊ฑฐ๋‚˜ ๋˜ ๋‹ค๋ฅธ async ํ•จ์ˆ˜ ์•ˆ์—์„œ await์„ ๋ถ™์—ฌ์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

async function findAndSaveUser(Users) {
  // ์ƒ๋žต
}

findAndSaveUser().then(() => { /* ์ƒ๋žต */ });
// ๋˜๋Š”
async function other() {
  const result = await findAndSaveUser();
}

 

 

# Map/Set

 

๋งต์€ ๊ฐ์ฒด์™€ ์œ ์‚ฌ Set์€ ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌ

๋งต์€ C++์—์„œ ๋ณธ๊ฑฐ๊ฐ™๊ณ  Set์€ ์Šค์œ„ํ”„ํŠธ์—์„œ๋„ ์ž˜๋ณด์ž„.

 

 - Map

์†์„ฑ๋“ค ๊ฐ„์˜ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฐ’๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , size๋„ ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ.

(๊ฐœ์ธ์˜๊ฒฌ) ๊ทธ๋ƒฅ ์ž๋ฃŒ๊ตฌ์กฐ๋ผ๊ณ  ์ƒ๊ฐ

 

const m = new Map();

m.set('a', 'b'); // set(ํ‚ค, ๊ฐ’)์œผ๋กœ Map์— ์†์„ฑ ์ถ”๊ฐ€ 
m.set(3, 'C'); // ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฐ’์„ ํ‚ค๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค

const d = {};

m.set(d, 'e'); // ๊ฐ์ฒด๋„ ๋ฉ๋‹ˆ๋‹ค
m.get(a); // get(ํ‚ค)๋กœ ์†์„ฑ๊ฐ’ ์กฐํšŒ

console.log(m.get(d)); // e

m.size; // size๋กœ ์†์„ฑ ๊ฐœ์ˆ˜ ์กฐํšŒ

console.log(m. size); // 3

for (const [k, v] of m) { // ๋ฐ˜๋ณต๋ฌธ์— ๋ฐ”๋กœ ๋„ฃ์–ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค
	console.log (k, v); // 'a', 'b', 3, 'C', 0, 'e'
} // ์†์„ฑ ๊ฐ„์˜ ์ˆœ์„œ๋„ ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค

m.forEach((v, k) => { // forEach๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค 
	console.log(k, V); // ๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๋™์ผ
});

m.has(d); // has(ํ‚ค)๋กœ ์†์„ฑ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ
console.log(d); // true

m.delete(d); // delete(ํ‚ค)๋กœ ์†์„ฑ์„ ์‚ญ์ œ
m.clear(); // clear()๋กœ ์ „๋ถ€ ์ œ๊ฑฐ

console.log(m.size); // 0

 

- Set

Set์€ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ.

const arr = [1, 3, 2, 7, 2, 6, 3, 5];

const s = new Set(arr); // ์ค‘๋ณต์ œ๊ฑฐ
const result = Array.from(s); // ๋‹ค์‹œ set์„ ๋ฐฐ์—ด๋กœ ๋˜๋Œ๋ฆฌ๊ธฐ

console.log(result); // 1, 3, 2, 7, 6, 5

 

 

# ๋„ ๋ณ‘ํ•ฉ/์˜ต์…”๋„ ์ฒด์ด๋‹

ES2020์—์„œ ๋‚˜์˜ด

falsy๊ฐ’: (0, '', false, NaN, null, undefined) 

๋„ ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž(||) ๋Š” falsy๊ฐ’์ค‘ null๊ณผ undefined๋งŒ ๋”ฐ๋กœ ๊ตฌ๋ถ„.

 

const a = 0;

const b = a || 3; // || ์—ฐ์‚ฐ์ž๋Š” falsy ๊ฐ’์ด๋ฉด ๋’ค๋กœ ๋„˜์–ด๊ฐ
console.log(b); // 3

const c = 0;
const d = c ?? 3; // ?? ์—ฐ์‚ฐ์ž๋Š” null์ผ๋•Œ๋ž‘ undetined์ผ ๋•Œ๋งŒ ๋’ค๋กœ ๋„˜์–ด๊ฐ
console.log(d); // 0;

const e = null;
const f = e ?? 3;
console.log(f); // 3;

const g = undefined;
const h = g ?? 3;
console.log(h); // 3;

 

์˜ต์…”๋„ ์ฒด์ด๋‹

 

์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž. ๊ทผ๋ฐ ์ด๊ฑด ๋ญ ์Šค์œ„ํ”„ํŠธ์—์„œ๋„ ์ดํ•ดํ–ˆ๋˜ ๊ฐœ๋…์ด๋ฏ€๋กœ ๋‹น์—ฐํ•˜๋‹ค ์‹ถ์Œ 

{}๋ฅผ ๊ฐ์ฒด๋กœ ํ‘œํ˜„ํ•˜๋Š”๋ฐ๋งŒ ์ฃผ๋ชฉํ•˜์ž.

const a = {}
a.b: // a๊ฐ€ ๊ฐ์ฒด์ด๋ฏ€๋กœ ๋ฌธ์ œ์—†์Œ

const c = null;
try {
	c.d;
} catch (e) {
	console.error(e); // TypeError: Cannot read properties of null (reading 'd')
}
c.d?; // ๋ฌธ์ œ์—†์Œ

try {
	c.f();
} catch (e) {
	console. error(e); // TypeError: Cannot read properties of null (reading 'f')
๏ฝ
c?.f(); ๋ฌธ์ œ์—†์Œ

try {
	c[0];
} catch (e) {
	console. error(e); // TypeError: Cannot read properties of null (reading '0')
}

 

 

#  AJAX

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
  // ์—ฌ๊ธฐ์— ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋„ฃ์œผ์„ธ์š”.
</script>

 

get ์š”์ฒญ ๋ณด๋‚ด๋ณด๊ธฐ

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
  axios.get('https://www.zerocho.com/api/get')
    .then((result) => {
      console.log(result);
      console.log(result.data); // {}
    })
    .catch((error) => {
      console.error(error);
    });
</script>

 

async await ์‚ฌ์šฉ

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
  (async () => {
    try {
      const result = await axios.get('https://www.zerocho.com/api/get');
      console.log(result);
      console.log(result.data); // {}
    } catch (error) {
      console.error(error);
    }
  })();
</script>

 

์ด๋ฒˆ์—๋Š” post ์—ฐ์Šต

 - ์ „์ฒด์ ์ธ ๊ตฌ์กฐ๋Š” ๋น„์Šทํ•˜๋‚˜ ๋ฐ”๋”” ๋ถ€๋ถ„์ด ์ถ”๊ฐ€๋˜์—ˆ์Œ.

 

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
  (async () => {
    try {
      const result = await axios.post('https://www.zerocho.com/api/post/json', {
        name: 'zerocho',
        birth: 1994,
      });
      console.log(result);
      console.log(result.data);
    } catch (error) {
      console.error(error);
    }
  })();
</script>

 

#  FormData

 

์ด๊ฒŒ ๋ญ๋ƒ๋ฉด HTML ํƒœ๊ทธ์˜ form ํƒœ๊ทธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์ ์œผ๋กœ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ.

์ฃผ๋กœ AJAX์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ๋‹ค.

๋จผ์ € FormData ์ƒ์„ฑ์ž๋กœ formdata ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ํ•œ ์ค„์”ฉ console ํƒญ์— ์ž…๋ ฅ.

<script>
  const formData = new FormData();
  formData.append('name', 'zerocho');
  formData.append('item', 'orange');
  formData.append('item', 'melon');
  formData.has('item'); // true
  formData.has('money'); // false;
  formData.get('item');// orange
  formData.getAll('item'); // ['orange', 'melon'];
  formData.append('test', ['hi', 'zero']);
  formData.get('test'); // hi, zero
  formData.delete('test');
  formData.get('test'); // null
  formData.set('item', 'apple');
  formData.getAll('item'); // ['apple'];
</script>

์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ append๋ฅผ ํ†ตํ•ด ํ‚ค-๋ฐธ๋ฅ˜๋กœ ๋ฐ์ดํ„ฐ ์ €์žฅ ๊ฐ€๋Šฅ.

 

์ด์ œ ํผ ๋ฐ์ดํ„ฐ๋ฅผ axios๋กœ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด๋ฉด๋œ๋‹ค.

๊ทธ๋ƒฅ ๋ฐ”๋””๋ฅผ ๋” ๊น”๋”ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•œ ๋Š๋‚Œ.

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
  (async () => {
    try {
      const formData = new FormData();
      formData.append('name', 'zerocho');
      formData.append('birth', 1994);
      const result = await axios.post('https://www.zerocho.com/api/post/formdata', formData);
      console.log(result);
      console.log(result.data);
    } catch (error) {
      console.error(error);
    }
  })();
</script>

 

#  encodeURIComponent, decodeURIComponent

 

AJAX์— ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ ์ฃผ์†Œ์— ํ•œ๊ธ€์ด ๋“ค์–ด๊ฐ„ ๊ฒฝ์šฐ ์„œ๋ฒ„์— ๋”ฐ๋ผ ํ•œ๊ธ€ ์ฃผ์†Œ๋ฅผ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด์„œ window๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ์ธ encodeURIComponent ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ

 

ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋Š” ๋…ธ๋“œ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

ํ•œ๊ธ€ ๋ถ€๋ถ„๋งŒ ๊ฐ์‹ธ๊ธฐ.

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
  (async () => {
    try {
      const result = await axios.get(`https://www.zerocho.com/api/search/${encodeURIComponent('๋…ธ๋“œ')}`);
      console.log(result);
      console.log(result.data); // {}
    } catch (error) {
      console.error(error);
    }
  })();
</script>

๋ฐ›๋Š”์ชฝ๋„ ์ธ์ฝ”๋”ฉ๋˜์–ด ๋ฐ›๋Š”๋ฐ

decodeURIComnponent('%EB....'); // ๋…ธ๋“œ

 

๋ฐ›๋Š”์ชฝ์—์„œ ๋””์ฝ”๋“œ ์ฒ˜๋ฆฌ๊ฐ€ํ•„์š”ํ•˜๋‹ค.

 

#  ๋ฐ์ดํ„ฐ ์†์„ฑ๊ณผ dataset

๋…ธ๋“œ๋ฅผ ์›น ์„œ๋ฒ„๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ํด๋ผ์ด์–ธํŠธ์™€ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ฒŒ ๋œ๋‹ค.

์ด๋•Œ ์„œ๋ฒ„์—์„œ ๋ณด๋‚ด์ค€ ๋ฐ์ดํ„ฐ๋ฅผ ํ”„๋ŸฐํŠธ์—”๋“œ ์–ด๋””์— ๋„ฃ์–ด์•ผ ํ• ์ง€ ๊ณ ๋ฏผ.

๊ฐ€์žฅ ๋จผ์ € ๊ณ ๋ฏผํ•ด์•ผํ•  ์ ์€ ๋ณด์•ˆ!

๋ฏผ๊ฐํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ ˆ๋Œ€ ๋ณด๋‚ด์ง€๋ง๊ณ , ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฐ™์€๊ฒƒ๋„ ๋‚ด๋ ค๋ณด๋‚ด์ง€ ๋ง๊ธฐ.

 

๋ณด์•ˆ๊ณผ ๊ด€๋ จ์ด ์—†๋Š” ๋ฐ์ดํ„ฐ๋Š” ์ž์œ ๋กญ๊ฒŒ ํ”„๋ŸฐํŠธํŠธ์—”๋“œ๋กœ ๋ณด๋‚ด๋„๋œ๋‹ค.

 

<ul>
  <li data-id="1" data-user-job="programmer">Zero</li>
  <li data-id="2" data-user-job="designer">Nero</li>
  <li data-id="3" data-user-job="programmer">Hero</li>
  <li data-id="4" data-user-job="ceo">Kero</li>
</ul>
<script>
  console.log(document.querySelector('li').dataset);
  // { id: '1', userJob: 'programmer' }
</script>

์œ„์™€๊ฐ™์ด HTML5 ํƒœ๊ทธ์˜ ์†์„ฑ์œผ๋กœ data-๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ๋“ค์„๋„ฃ๋Š”๋‹ค.

 

์ฃผ์„์„ ๋ณด๋ฉด ์กฐ๊ธˆ์”ฉ ๋ณ€ํ˜• ๋˜์—ˆ๋Š”๋ฐ, data- ์ ‘๋‘์–ด๋Š” ์‚ฌ๋ผ์ง€๊ณ  - ๋’ค์— ์œ„์น˜ํ•œ ๊ธ€์ž๋Š” ๋Œ€๋ฌธ์ž๊ฐ€ ๋œ๋‹ค. data-id๋Š” id, data-user-job์€ userJob์ด ๋˜์—ˆ๋‹ค.

 

๋ฐ˜๋Œ€๋กœ dataset์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด๋„ HTML์— ๋ฐ˜์˜๋œ๋‹ค. 

 

dataset.monthSalary = 10000;์„ ๋„ฃ์œผ๋ฉด data-moth-salary = "10000" ์ด๋ผ๋Š” ์†์„ฑ์ด ์ƒ๊น€

Comments