W5D1 Node.js Introduction, Sync & Async, Blocking & Non-blocking, Promise, Object Literal, Error Handling

Jin Bae·2022년 12월 12일
0

스파르타코딩클럽

목록 보기
18/35

Node.js Introduction

Node.js의 특징

  • Non-blocking
    - Blocking I/O waits until the function in the program returns a result while Non-blocking I/O can execute other functions at the same time
  • Single thread
    - Uses one thread and can only execute another function at the same time
  • Event loop
    - A characteristic (logic) can that effectively execute functions to overcome the disadvantage of single thread

REPL

  • Read, Evaluate, Print, Loop
    - Read the inputted code and save it in memory
    • Evaluate the value
    • Print the value
    • Loop until a certain signal is given
  • Allows us to quickly confirm the value of a code and is appropriate for checking code syntax or output

Sync & Async (동기와 비동기)

  • Sync: Waits until the running code outputs a result
  • Async: Outputs a result regardless of the order of which the function executed

Blocking & Non-blocking Model

  • Blocking: Does not give control until the code is over and waits until it is over

  • Non-blocking: Gives control to another code so that the other code can execute even though the first code has not finished executing

  • Javascript uses Async + Non-blocking model to return the next code's result even though the current code has not finished executing

Promise

  • Promise is an object type that helps execute in sync
  • Composed of:
  1. executor: Only accepts functions. Automatically runs when the Promise is made. Must call either resolve or reject
  2. resolve: returned when the Promise is successfully run
    • Runs .then when the function is resolved
  3. reject: returned when there is an error
    • Runs .catch when the function is rejected
  • await: Stops executing the function until the Promise is either fulfilled or rejected
new Promise(executor);

// 예제
new Promise((resolve, reject) => {
	// 명령문
});
  • 3 types of Promise status:
  1. Pending (대기): Initial status, when the code hasn't run or hasn't been rejected
  2. Fulfilled (이행): The code has successfully run
  3. Rejected (거부): The code has failed

Object Literal

  • Data types in Javascript can largely be categorized into primitive and object types
    - Primitive type: only represents one value and cannot be changed
    • Object type: represents various type values into one unit. Is a complex data structure. The value can be changed. Is made up of 0 or more properties and one property is made up of a key and value.
  • Object Literal: A notational system to create an object
    - Is a singleton, that means that when a change is made to an object made with object literal, it will change the object throughout the script
    • As opposed to an object made with a constructor that will not change the object throughout the script if a change is made to the object

Error handling

  • Use error handling to handle expected and unexpected errors
  • There are usually more unexpected errors and a program must be ready to handle them

try/catch

Use try/catch to handle errors.

const users = ["Lee", "Kim", "Park", 2];

try {
  for (const user of users) {
    console.log(user.toUpperCase());
  }
} catch (err) {
  console.error(`Error: ${err.message}`);
}

// LEE
// KIM
// PARK
// Error: user.toUpperCase is not a function

throw

Throw an error on purpose when, for example, if an input is invalid.

If a user tries to withdraw more than the balance, throw an error:

function withdraw(amount, account) {
  if (amount > account.balance)
    throw new Error("잔고가 부족합니다.");
  account.balance -= amount;
	console.log(`현재 잔고가 ${account.balance}남았습니다.`); // 출력되지 않음
}

const account = { balance: 1000 };
withdraw(2000, account);

// Error: 잔고가 부족합니다.

finally

Executes regardless if an error has occurred or not

0개의 댓글