JavaScript Basic
ํ์ด์ง๐๋ฅผ ๋์ ์ผ๋ก ๋ง๋ค์ด์ฃผ๊ธฐ ์ํด ํ์ํ JavaScript!! ๊ทธ ์ฌ์ฉ๋ฒ์ ๋ฑ๋ฑ์ด ํํด์ณ๋ณด ์!
Variableโ
var
let / const
-
์ ์ธ๊ณผ ํ ๋น
-
var
var myVar; // ์ ์ธ
myVar = 1; // ํ ๋น
var myVar = "Hello"; // ์ฌ์ ์ธ ๊ฐ๋ฅ -
let
let myLet; // ์ ์ธ
myLet = 2; // ํ ๋น
myLet = 3; // ์ฌํ ๋น ๊ฐ๋ฅ
let myLet = "hi" // SyntaxError -> ์ด๋ฏธ ์ ์ธ๋จ -
const
const myConst // SyntaxError -> ์ด๊ธฐํ ๋๋ฝ
const myConst = "hi"; // ๋ฐ๋์ ๊ฐ๊ณผ ํจ๊ป ์ ์ธ
myConst = "bye" // TypeError -> ์ฌํ ๋น ๋ถ๊ฐ
-
-
Scope
-
var
let VarFunction = function () {
var myVar = 0;
if (true) {
var myVar = 1;
console.log(myVar); // 1
}
console.log(myVar); // 1
}; -
let
let LetFunction = function () {
let myLet = 0;
if (true) {
let myLet = 1;
console.log(myLet); // 1
}
console.log(myLet); // 0
}
-
Hoistingโ
-
var
console.log(mooyeon); // undefined
var mooyeon = "๋ฌด์ฐ";์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ด ์ดํดํ๋ค.
var mooyeon; // hoisting
console.log(mooyeon);
mooyeon = "๋ฌด์ฐ"; -
let
console.log(dooly); // ReferenceError - ์ด๊ธฐํํ๊ธฐ ์ ์ ๊ทผ ๊ธ์ง
let dooly = "๋๋ฆฌ"; -
์ดํดํ๊ธฐ
-
var
- ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ
- ํ ๋น
-
let
,const
TDZ(temporal Dead Zone)์ด ์กด์ฌ
- ์ ์ธ
- TDZ
- ์ด๊ธฐํ
- ํ ๋น
-
Typeโ
- ์์ํ์ (primitive data type)
- boolean
- null
- undefined
- number
- string
- symbol (ES6+)
- ๊ฐ์ฒดํ์ (object)
- object
-
Number
3 - 5;
Infinity; // typeof Infinity >> "number"
NaN; // typeof NaN >> "number" ์ฐ์ ์ฐ์ฐ ๋ถ๊ฐ, number๊ฐ ์๋๋ผ๋ number..
10 / 0; // Infinity
0 / 0; // NaN -
String
let myName = "๋ฌด์ฐ";
myName = '๋ฌด์ฐ';
// ` (backtick) : ES6+, ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด
// string interpolation, ์ค๋ฐ๊ฟ(๊ฐํ) - ์ํฐ๋ ์ธ์ํ์ฌ ์ถ๋ ฅ๋๋ค.
myName = `๋ฌด
์ฐ`; -
Boolean
true;
false; -
Empty value
undefined; // typeof undefined >> "undefined"
null; // typeof null >> "object"
Operatorโ
-
๋๋ฑ ์ฐ์ฐ์ (
==
)1 == '1'; // true
-
์ผ์น ์ฐ์ฐ์ (
===
)1 === '1' // false
-
ํ ๋น
=
,+=
,-=
,*=
,/=
, ...
-
๋น๊ต
>
,<
,>=
,<=
, ...
-
๋ ผ๋ฆฌ
- and :
&&
- or :
||
- and :
-
Not (
!
) -
์ผํญ์ฐ์ฐ์ ํํ์
? true : false
2 > 4 ? true : false
Flowโ
-
if๋ฌธ
if (userName === "ssafy") {
message = "<h1>Hello ssafy</h1>";
} else if (userName === "1q2w3e4r") {
message = "<h1>Admin page์ ๋๋ค</h1>";
} else {
message = `<h1>${userName} ํ์ํฉ๋๋ค.</h1>`;
} -
switch๋ฌธ
switch (userName) {
case "1q2w3e4r": {
message = "<h2>๊ด๋ฆฌ์๋ ์ถฉ์ฑ์ถฉ์ฑ</h2>";
break;
}
case "ssafy": {
message = '<a href="http://edu.ssafy.com">์ธํผ</a>';
break;
}
default: {
message = `<h1>${userName} ํ์ํฉ๋๋ค.</h1>`;
}
} -
for๋ฌธ
/*
*๋ฐ๋ณต๋ฌธ
var๋ ํจ์์ค์ฝํ๋ฅผ ๊ฐ์ง๊ณ ์์ด์ for๋ฌธ ์ดํ์ i ๋ณ์์ ๊ฐ์ด ์ ์ง
let์ ๋ธ๋ก์ค์ฝํ๋ฅผ ๊ฐ์ง๊ณ ์์ด์ for๋ฌธ ์ดํ์ ํด๋น ๋ณ์๋ ์์.
*/
for (var i = 0; i < 3; i++) {
console.log(i);
}
console.log(i); // 3
for (let i = 0; i < 3; i++) {
console.log(i);
}
console.log(i); // ReferenceErrorlet myArray = ["๋ฌด์ฐ", "ํํ", "์๋ฒ"];
for (let name of myArray) {
document.write(name);
}
Functionโ
-
์ ์ธ์๊ณผ ํํ์
console.log(myAdd(2, 3)); // 5
console.log(myAdd2(3, 5)); // ReferenceError: myAdd2 is not defined
// ์ ์ธ์ -> hoisting ๋๋ค
function myAdd(a, b) {
return a + b;
}
// ํํ์ -> hoisting ์๋๋ ์ฌ๋งํ๋ฉด ํํ์์ผ๋ก ํจ์ ๋ง๋ค์.
let myAdd2 = function (a, b) {
return a + b;
}; -
Arrow function
let myFunction = function (a) {
return a + 1;
};
// 1. function ํค์๋ ์ญ์ ํ =>
let myArrowFunction1 = a => {
return a + 1;
};
// 1-1. ์ธ์๊ฐ ํ๋๋ผ๋ฉด, ์๊ดํธ ์๋ต ๊ฐ๋ฅ
// 1-2. ๋ฌธ์ฅ์ด ํ ์ค์ด๊ณ , ๋ฆฌํด์ด๋ผ๋ฉด ์ค๊ดํธ ๋ฐ return ํค์๋ ์๋ต ๊ฐ๋ฅ
let myArrowFunction2 = (a) => a + 1;
// 1-3. ์ธ์๊ฐ ์๋ ๊ฒฝ์ฐ์๋ () or _
let ArrowGreeting2 = () => {
console.log("happy!");
}; -
๊ธฐ๋ณธ ์ธ์
let greeting2 = (name = "ํํค") =>
name(
/*
* ์ต๋ช ํจ์
*/
(function (a) {
return a * 10;
})(10)
)((a) => a + 10)(10);
Arrayโ
-
Array ์ ์ธ
let numbers = [10, 1, 3, 5];
-
Index ์์ ์ ๊ทผ
numbers[0]; // 10
numbers[-1]; // undefined
numbers.length; // 4 -
๋ค์ง๊ธฐ
numbers.reverse(); // return + ์๋ณธ ๋ณ๊ฒฝ.
-
๋ง์ง๋ง์ ์์ ์ถ๊ฐ
numbers.push(20); // ๋ง์ง๋ง์ ์์ ์ถ๊ฐ + return (๊ธธ์ด)
-
๋ง์ง๋ง ์์ ์ ๊ฑฐ ๋ฐ
return
numbers.pop(); // 20 : ๋ง์ง๋ง ์์ pop + return (ํด๋น ์์)
-
๊ฐ์ฅ ์์ ์์ ์ถ๊ฐ ๋ฐ ๊ธธ์ด
return
numbers.unshift(3); // ๋งจ์์ ์์ ์ถ๊ฐ + return (๊ธธ์ด)
-
๋งจ์ ์์ ์ ๊ฑฐ ๋ฐ
return
numbers.shift(); // ๋งจ์ ์์ ์ ๊ฑฐ + return (ํด๋น ์์)
-
ํฌํจ ์ฌ๋ถ ํ์ธ
numbers.includes(1); // true : ํฌํจ์ฌ๋ถ ํ์ธ
-
ํฌํจ ์ฌ๋ถ + ๊ฐ์ฅ ๋จผ์ ์กด์ฌํ๋ ์์น ๋ฐํ
numbers.indexOf(1); // ๊ฐ์ฅ ๋จผ์ ์กด์ฌํ๋ ์์น
-
String์ผ๋ก ๋ฐ๊พธ๊ธฐ
numbers.join(); // ๊ธฐ๋ณธ์ด ,
numbers.join("-"); // -๋ก ์ฐ๊ฒฐ.
Objectโ
-
Object ์์ฑ
// ํค๊ฐ์ ""๋ก ๊ตณ์ด ์๋ฌถ์ด๋ ๋๋ค.
const me = {
// ํ๋กํผํฐ
name: "kim",
"phone number": "01012345678",
phone: {
type: "iphone XS MAX",
},
// ๋ฉ์๋ function ํค์๋๋ง ์์ฑํ์!
greeting: function () {
console.log(this); // me
console.log(`hi ${this.name}`); // this : '๋'๋ฅผ ๋ปํ๋ค. self.name์ฒ๋ผ
},
greeting2: () => {
console.log(this); // ์ ์ญ๊ฐ์ฒด window
console.log(`hi ${this.name}`); // ์ด๊ฑด this๊ฐ ์๋จนํ๋ค!? arrow function์์์ this๋ ๋ฌด์กฐ๊ฑด ์์ object! ๋์ค๊ฐ๋ฉด ์ ์ฉํ ๊ฑธ!?
},
};// ํจ์์ ์ธ์ผ๋ก ์ฐ๋ฆฌ๊ฐ ์ต์ํ ํํ์ object ์์ฑ ๊ฐ๋ฅ
// ์ธ์๋ฅผ ๋ฐ์์ ๋ญ๊ฐ๋ฅผ ํ๋, ๋ค์ํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ์ถ์ ๋.
// ๋ค์๊ณผ ๊ฐ์ด ํจ์๋ก ๋ง๋ค์ด ๋๊ณ ''new'' ํค์๋๋ก ๋ถ๋ฌ์ ๊ฐ์ฒด ์์ฑ๊ฐ๋ฅ.
const Person = function(name, phone) {
this.name = name
this.phone = phone
this.greeting = function() {
return 'hi' + this.name
}
}
const moo = new Person('์ต๋ฌด์ฐ', '010-????-????') // new! ํน์ ํ ์ค๋ธ์ ํธ๋ฅผ ๋ง๋๋ ํค์๋
lee.name
lee.greeting()
// ๊ทธ๋ผ arrow func ์ผ๋ก๋ ๋ ๊น?
const Animal = name => {
this.name = name
}
const dog = new Animal('dog') // Error!!!!!!!!!
// ์์ฑ์ ํจ์์์๋ arrow func ์ฌ์ฉ ๊ธ์ง
// object ๋ฆฌํฐ๋ด
const name = '๊ฒจ๋ '
const phone = '010-0000-2222'
const greeting = function () {
return 'hi,' + this.name
}
const you = {
name,
phone,
greeting
}
//์ด๋ฐ์์ผ๋ก ์ฐ๋๊ฒ ์ค๋ธ์ ํธ ๋ฆฌํฐ๋ด ๋ฐฉ๋ฒ. ์ถ์ฝํ์ด๋ค ๋ผ๊ณ ๊ฐ๋จํ ์๊ฐํ๋ฉด ๋๋ค. -
์ค๋ธ์ ํธ ๋ฆฌํฐ๋ด (ES6+)
let book = "์๋ฐ์คํฌ๋ฆฝํธ ์์ ์ ๋ณต";
let album = {
IU: ["์ข์๋ ", "๋ฐคํธ์ง"],
BTS: ["์์๊ฒ๋ค์ ์ํ ์"],
};
// ์์ ๊ฐ๋ค์ object์ ๋ฃ์ผ๋ฉด ๋ฐ๋ก key,value๋ก ์์์ ๋ค์ด๊ฐ๋ค.
let bag = {
book,
album,
}; -
JSON
// JSON (Javascript Object Notation - ์๋ฐ์คํฌ๋ฆฝํธ ์ค๋ธ์ ํธ ํ๊ธฐ๋ฒ)
// JSON์ ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ฐ์คํฌ๋ฆฝํธ ์ค๋ธ์ ํธ ํ๊ธฐ๋ฒ์ ๊ฐ์ง "๋ฌธ์์ด"์ด๋ค
// object -> JSON
let jsonData = JSON.stringify(me);
let myObject = JSON.parse(jsonDate);
Array helper methodโ
-
Array.forEach
let numbers = [1, 2, 3];
// 1. ๋ฐ๋ณต๋ฌธ (for)
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
// 2. ๋ฐ๋ณต๋ฌธ (for..of)
for (let num of numbers) {
console.log(num);
}
// 3. forEach
numbers.forEach(function (num) {
console.log(num);
}); -
map
์ฝ๋ฐฑํจ์์ return ๊ฒฐ๊ณผ๋ฅผ ๊ฐ๊ฐ ์์๋ก ํ๋ ๋ฐฐ์ด์ ๋ฆฌํด
let numbers = [1, 2, 3];
let doubleNumbers = numbers.map(function (number) {
return number * 2;
});
console.log(doubleNumbers);
let numbers = [1, 2, 3];
let doubleNumbers = numbers.map((number) => number * 2);
console.log(doubleNumbers); -
filter
callback ํจ์์ return ๊ฒฐ๊ณผ๊ฐ ์ฐธ์ธ ๊ฒ์ ๊ฐ๊ฐ ์์๋ก ํ๋ ๋ฐฐ์ด์ ๋ฆฌํด
// images์ ๋์ด๊ฐ 100๋ณด๋ค ์์ object๋ง ๋ด์ ๋ฐฐ์ด
// ๋ฐ๋ณต๋ฌธ์ผ๋ก
let list = [];
for (image of images) {
if (image.height < 100) {
list.push(image);
}
}
console.log(list);
// filter๋ก
list = images.filter(function (image) {
return image.height < 100;
});
console.log(list); -
reduce(callbackfn, initialvalue)
return ๊ฒฐ๊ณผ๋ฅผ ๋์ ํ ๊ฒฐ๊ณผ๋ฅผ return
let mySsafy = [100, 100, 95, 90];
let total = 0;
mySsafy.forEach(function (score) {
total += score;
});
mySsafy.reduce(function (total, score) {
// total: ๋์ , score: ๊ฐ ์์
return total + score;
}, 10000); // 10000 :์ด๊ธฐ๊ฐ, reduce์ ๋๋ฒ์งธ ์ธ์. ์์ผ๋ฉด ์ฒซ score๋ถํฐ ์์ํด์ return
mySsafy.reduce((total, score) => total + score, 10000); -
find
์ผ์นํ๋ ์ฒซ๋ฒ์งธ ์์๋ฅผ return.
mySsafy.find(function (score) {
return score === 90;
}); -
some
,every
// some: return์ ๋ง์กฑํ๋๊ฒ ํ๋๋ผ๋ ์๋๋
let myNumbers = [1, 2, 3, 4];
myNumbers.some(function (number) {
return number % 2 === 0;
});
// every: ๋ชจ๋ ๊ฒ์ด return์ ๋ง์กฑํ๋๋
myNumbers.every(function (number) {
return number % 2 !== 0;
});
DOMโ
<body>
<h1 class="text">DOM ์กฐ์(Document Object Model)</h1>
<h2 class="text">์นดํ</h2>
<img src="my.png", alt="">
<ul id='my-list'>
<li>์๋ฉ๋ฆฌ์นด๋
ธ</li>
<li>์๋ฐ์นฉํ๋ผํ์น๋
ธ</li>
</ul>
</body>
Blockingโ
-
setTimeout
๊ณผ Callbackfunction a() {
console.log("a");
}
console.log("hi");
setTimeout(a, 3000);
console.log("bye"); // hi -> bye -> (3์ด๋ค) -> a
function printHello() {
console.log("Hello");
}
function baz() {
console.log("baz"); // 1
setTimeout(printHello, 3000); // 3- ๋น๋๊ธฐ๋ก ๋์ํ๋ ํจ์์ด๋ค. 3000์ด ์๋๋ผ 0์ด์ด๋ Hello๊ฐ ๋ง์ง๋ง์ ์ถ๋ ฅ๋๋ค..!!
console.log("baz end"); //2
}
function bar() {
console.log("bar");
baz();
}
function foo() {
console.log("foo");
bar();
}
foo();
function sum(x, callbackfn) {
setTimeout(callbackfn, 1000, x + 1);
}
var result = 0;
sum(2, function (x) {
result = x;
console.log(result); // 3
});
console.log(result); // 0
Event Loopโ
- Blocking๊ณผ ๊ฐ์ด ์ ๋ณด์..
- ์ด๋ฒคํธ ๋ฃจํ :
call stack
,callback queue
ํ์ธํ๋ ์ญํcall stack
์ด ๋น์ด์์ผ๋ฉด,callback queue
์์call stack
์ผ๋ก ์ด๋.- ์ด๋ฒคํธ(ํจ์์คํ..)
- tick(ํฑ)
- synchronous / Asynchronous (๋๊ธฐ / ๋น๋๊ธฐ)
- Synchronous/Asynchronous๋ ํธ์ถ๋๋ ํจ์์ ์์ ์๋ฃ ์ฌ๋ถ๋ฅผ ๋๊ฐ ์ ๊ฒฝ์ฐ๋๊ฐ ๊ด์ฌ์ฌ๋ค.
- ํธ์ถ๋๋ ํจ์์๊ฒ callback์ ์ ๋ฌํด์, ํธ์ถ๋๋ ํจ์์ ์์ ์ด ์๋ฃ๋๋ฉด ํธ์ถ๋๋ ํจ์๊ฐ ์ ๋ฌ๋ฐ์ callback์ ์คํํ๊ณ , ํธ์ถํ๋ ํจ์๋ ์์ ์๋ฃ ์ฌ๋ถ๋ฅผ ์ ๊ฒฝ์ฐ์ง ์์ผ๋ฉด Asynchronous๋ค.
- ํธ์ถํ๋ ํจ์๊ฐ ํธ์ถ๋๋ ํจ์์ ์์ ์๋ฃ ํ ๋ฆฌํด์ ๊ธฐ๋ค๋ฆฌ๊ฑฐ๋, ๋๋ ํธ์ถ๋๋ ํจ์๋ก๋ถํฐ ๋ฐ๋ก ๋ฆฌํด ๋ฐ๋๋ผ๋ ์์ ์๋ฃ ์ฌ๋ถ๋ฅผ ํธ์ถํ๋ ํจ์ ์ค์ค๋ก ๊ณ์ ํ์ธํ๋ฉฐ ์ ๊ฒฝ์ฐ๋ฉด Synchronous๋ค.
- blocking / non-blocking
- Blocking/NonBlocking์ ํธ์ถ๋๋ ํจ์๊ฐ ๋ฐ๋ก ๋ฆฌํดํ๋๋ ๋ง๋๋๊ฐ ๊ด์ฌ์ฌ๋ค.
- ํธ์ถ๋ ํจ์๊ฐ ๋ฐ๋ก ๋ฆฌํดํด์ ํธ์ถํ ํจ์์๊ฒ ์ ์ด๊ถ์ ๋๊ฒจ์ฃผ๊ณ , ํธ์ถํ ํจ์๊ฐ ๋ค๋ฅธ ์ผ์ ํ ์ ์๋ ๊ธฐํ๋ฅผ ์ค ์ ์์ผ๋ฉด NonBlocking์ด๋ค.
- ๊ทธ๋ ์ง ์๊ณ ํธ์ถ๋ ํจ์๊ฐ ์์ ์ ์์ ์ ๋ชจ๋ ๋ง์น ๋๊น์ง ํธ์ถํ ํจ์์๊ฒ ์ ์ด๊ถ์ ๋๊ฒจ์ฃผ์ง ์๊ณ ๋๊ธฐํ๊ฒ ๋ง๋ ๋ค๋ฉด Blocking์ด๋ค.
Axiosโ
axios.get("https://jsonplaceholder.typicode.com/posts/1").then((response) => {
console.log(response);
document.write(`
<h1>${response.data.id} : ${response.data.title}</h1>
<p>${response.data.body}</p>
`);
return response.data;
});
console.log("bye");
Promiseโ
๋ฐ์ดํฐ๋ฅผ ์ธ๋ถ๋ก ๋ถํฐ ๋ฐ์์์ ๋ณ์์ ์ ์ฅํ๊ณ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ๋ง๋ค์ด๋ณด์!
-
๋น๋๊ธฐ๊ฐ ์๋ ์ํ์ ์ฝ๋
function getData() {
const data = { data: "some data" };
return data;
}
let response = getData();
console.log(response); -
setTimeout
function getData() {
let data;
setTimeout(function () {
console.log("์์ฒญ์ ๋ณด๋์ต๋๋ค.");
data = { data: "some data" };
}, 1000);
return data;
}
let response1 = getData();
console.log(response1); // undefined- data๋ ๊ทธ๋ผ ์ด๋๋ก ๊ฐ๋๊ฐ!
-
callback func ์ ์
function getDataCallback(callback) {
setTimeout(function () {
console.log("์์ฒญ์ ๋ณด๋์ต๋๋ค");
const data = { data: "some data" }; // ๋ฐ์ดํฐ ๋์ฐฉ! ํ๋ฉด
callback(data); // ์ด๋! ๋ด๊ฐ ์ํ๋ ์์ (data๋ด์์ ์ถ๋ ฅ)์ ์์!
}, 1000);
}
// ํจ์ ํธ์ถ. ์ธ์๋ก 'ํจ์'๋ฅผ ๋๊ฒจ์ฃผ๋๋ฐ, ๊ทธ๊ฒ ์ถ๋ ฅํ๋ ์์ ์ ํ๋ ํจ์. ์~๋ฅผ ๋์ค์ callback ํ๊ฒ ๋ค..!
getDataCallback(function (data) {
let response2 = data;
console.log(response2);
});- ๊ทธ๋ฐ๋ฐ ์ด๊ฑด ์ฝ๋ฐฑ์ง์ฅ์ ๋ง๋ ๋ค...
-
promise
function getDataPromise() {
// Promise ์ค๋ธ์ ํธ๋ฅผ ๋ฆฌํดํ๋ ์ด๋ฐ๊ฒ ์๊ฒผ๋ค
return new Promise((resolve) => {
setTimeout(function () {
console.log("์์ฒญ์ ๋ณด๋์ต๋๋ค");
const data = { data: "some data" };
resolve(data);
}, 1000);
});
}
let response3 = getDataPromise();
// ์ด๋ฌ๋ฉด response3์ promise ์ค๋ธ์ ํธ๊ฐ ๋ฆฌํด๋๋ค๊ณ !
console.log(response3); // 1. ์ถ๋ ฅ์ pending์ด๋ผ๊ณ ๋๋ค. (์์ฒญ์ฒ๋ฆฌ์ค..?)
console.log(response3); // 2. ์ด์ ์ถ๋ ฅ์ด resolved๋ผ๊ณ ๋๋ค. (resolveํ์ด, ํจ์ ํธ์ถํ์ด ์ํ)
// ๊ทธ๋ฌ๋ฉด resolved ๋ญ ์ด๋ ๊ฒ ๋ด๊ธฐ๋๋ฐ ๊ทธ๊ฒ์ ํ์ด์ฃผ๋ ๊ฒ์ด .then
response3.then((response) => console.log(response)); // 3. data ์ถ๋ ฅ
// ๊ทธ๋์ ๋ค์๊ณผ ๊ฐ์ด ์ธ ์ ์๋ค.
getDataPromise().then((response) => console.log(response));
// ์ด๊ฒ ๋๋ถ์ .then .then .then ํ๋ฉด์ ์์ ์ ์งํํ ์ ์๊ฒ ๋์๋ค- ์ฝ๋ฐฑ์ง์ฅ์ ํด๊ฒฐํ๊ธฐ ์ํด ๋์จ Promise!
-
Promise ์ถ๊ฐ ๋ด์ฉ
function myPromise(url) {
return new Promise((resolve, reject) => {
// resolve ํ๋๋ง ์๋๋ผ reject๋ ๊ฐ์ด ๋ฃ์ ์ ์๋๋ฐ
if (url === "http") {
resolve("์ฑ๊ณต");
} else {
reject("url์ด ์๋ชป๋์์ต๋๋ค.");
}
});
}
const promise1 = myPromise("http");
console.log(promise1);
const promise2 = myPromise("www");
console.log(promise2);- resolve๋ก ๊ฐ๋ฉด ๊ทธ ๋ค์์ผ๋ก .then์ผ๋ก ๊ฐ๊ณ
- reject๋ก ๊ฐ๋ฉด ๊ทธ ๋ค์์ผ๋ก .catch๋ก ๊ฐ๋ค. (์๋ฌด๋ฆฌ .then์ด ๋ง์๋ creject๋ก ๊ฐ๋ฉด .then์ ๋ฌด์ํ๋ค๋ ๋ง)
promise2
.then((response) => {
// ์ด ๋ถ๋ถ์ ์คํ์ด ์๋๋ค๊ณ !
console.log("์ฑ๊ณต");
console.log(response);
})
.catch((error) => {
console.log("error");
console.log(error);
}); -
chaining
function getDataPromise() {
// Promise ์ค๋ธ์ ํธ๋ฅผ ๋ฆฌํดํ๋ ์ด๋ฐ๊ฒ ์๊ฒผ๋ค
return new Promise((resolve) => {
setTimeout(function () {
console.log("์์ฒญ์ ๋ณด๋์ต๋๋ค");
const data = { data: "some data" };
resolve(data);
}, 1000);
});
}
getDataPromise()
.then((response) => {
// response = data
console.log(response); // {'data': 'some data'}
return response.data; // 'some data'
})
.then((data) => {
// data = 'some data'
console.log(data); // 'some data'
})
.catch((error) => {
console.log(error);
}); -
chaining 2
- axios์์ ์ฌ์ฉ
axios
.get("https://jsonplaceholder.typicode.com/posts/1")
.then((response) => {
return response.data.userId; // ์ค์ ๋ก๋ promise object๋ก ํฌ์ฅ๋์ด ๋ฆฌํด
})
.then((userId) => {
return axios.get(`https://jsonplaceholder.typicode.com/users/${userId}`);
})
.then((response) => {
console.log(response);
console.log(response.data.name);
}); -
async / await
๋๊ธฐ ์์ ์ธ์ฒ ํ๊ธฐ!!!๊ฐ ํต์ฌ. + promise (promise๊ฐ ๋ฆฌํด์ธ ํจ์๋ฅผ ๋์์ผ๋ก๋ง ๊ฐ๋ฅํ๋ค.)
function getDataPromise() {
// Promise ์ค๋ธ์ ํธ๋ฅผ ๋ฆฌํดํ๋ ์ด๋ฐ๊ฒ ์๊ฒผ๋ค
return new Promise((resolve) => {
setTimeout(function () {
console.log("์์ฒญ์ ๋ณด๋์ต๋๋ค");
const data = { data: "some data" };
resolve(data);
}, 1000);
});
}
async function printData() {
const response = await getDataPromise();
console.log(response);
}
printData(); // {data: "some data"}- ์์์ jsonplceholder๋ฅผ ์ด๋ ๊ฒ ํ ์ ์๋ค!
- ๋๊ธฐ์ฒ๋ผ... ์ฝ๋๋ฅผ ์งค ์ ์์ง...? ๋ ์ฅ์ ์ด... ์ค๋ฅ ์์ ์ด ๋ ์ ํํ๊ฒ ๋์จ๋ค...
async function printUser() {
try {
// resolve ํธ์ถ ๋๋ฉด.
const response = await axios.get(
"https://jsonplaceholder.typicode.com/posts/1"
);
const userId = response.data.userId;
console.log(userId);
} catch (error) {
console.log(error); // rejected ํธ์ถ๋๋ฉด.
}
}
printUser();