๐ŸŸจ JavaScript

    [๋“œ๋ฆผ์ฝ”๋”ฉ] ์ฝœ๋ฐฑํ•จ์ˆ˜ (2) : ์ฝœ๋ฐฑ์ง€์˜ฅ ํƒˆ์ถœ, Promise (State, Producing)

    Promise Promise๋Š” javascript์— ๋‚ด์žฅ๋˜์–ด์žˆ๋Š” object๋กœ, ์ด๋ฅผ ํ†ตํ•ด์„œ ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋„คํŠธ์›Œํฌํ†ต์‹ ์„ ํ•œ๋‹ค๋˜์ง€, ํŒŒ์ผ์„ ์ฝ์–ด์˜จ๋‹ค๋˜์ง€ ํ•˜๋Š” ๋“ฑ์˜ ๋ฌด๊ฑฐ์šด ์ž‘์—…์„ ํ•  ๋•Œ๋Š” ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋ฏ€๋กœ Promise๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. ๋ฌด๊ฑฐ์šด ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ๋„ ์ผ์–ด๋‚˜๋Š” ๋ถˆํ•„์š”ํ•œ ๋„คํŠธ์›Œํฌํ†ต์‹ (promiseํ•จ์ˆ˜์˜ ์‹คํ–‰)์„ ๋ฐฉ์ง€ํ•ด์•ผ ํ•œ๋‹ค. new Promise๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋Š” ์ˆœ๊ฐ„, ํ•ด๋‹น executor ํ•จ์ˆ˜๊ฐ€ ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๋ฏ€๋กœ ์ฃผ์˜ํ•˜์ž. promise๋Š” ์•„๋ž˜ ๋‘ ๊ฐ€์ง€๋ฅผ ์—ผ๋‘์— ๋‘๋ฉด ์ข‹๋‹ค. 1. State(์ƒํƒœ) process๊ฐ€ ๋ฌด๊ฑฐ์šด operation์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋Š”์ง€/์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด ์„ฑ๊ณตํ–ˆ๋Š”์ง€/์‹คํŒจํ–ˆ๋Š”์ง€์˜ ์ƒํƒœ pending : operation์„ ์ˆ˜ํ–‰ ์ค‘์ธ ์ƒํƒœ ..

    [๋“œ๋ฆผ์ฝ”๋”ฉ] ์ฝœ๋ฐฑํ•จ์ˆ˜ (1) : ์ฝœ๋ฐฑ์ง€์˜ฅ ์˜ˆ์‹œ

    ์ฝœ๋ฐฑ์ง€์˜ฅ // ๊ธฐ๋ณธ์„ธํŒ… class UserStorage { loginUser(id, password, onSuccess, onError) { setTimeout(() => { if ( (id === 'master' && password === 'master00') || (id === 'submaster' && password === 'submaster00') ) { onSuccess(id); } else { onError(new Error('not found')); } }, 2000); } getRoles(user, onSucces, onError) { setTimeout(() => { if (user === 'master') { onSuccess({name: 'Master', role : 'admin'..

    [mdn, ๋“œ๋ฆผ์ฝ”๋”ฉ, ์ฝ”๋”ฉ์• ํ”Œ] ์ฝœ๋ฐฑํ•จ์ˆ˜ (1) : ๋™๊ธฐ์™€ ๋น„๋™๊ธฐ ๊ฐœ๋…

    ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋™๊ธฐ์ (synchronous) ์–ธ์–ด์ด๋‹ค. ์ด ๋ง์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ์ˆœ์„œ์— ๋งž์ถฐ ๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค๋Š” ๋ง์ด๋‹ค. ์•„๋ž˜๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. console.log('1'); setTimeout(() => console.log('2'), 1000); console.log('3'); //1 //3 //2 Synchronous callback function printImmediately(print) { print(); } printImmediately(() => console.log('hello')); Asynchronous callback function printWithdelay(print, timeout) { setTimeout(print, timeout); } printW..

    [๋“œ๋ฆผ์ฝ”๋”ฉ] JSON to Object (parse)

    JSON to Object parse(json) const coder = { name: 'Noran', language: 'Javascript', height: null, birthDate: new Date(), sayhi: () => { console.log(`Hello, I'm ${name}!`); }, }; json = JSON.stringify(coder); const obj = JSON.parse(json); console.log(obj); //{name: 'Noran', language: 'Javascript', height: null, birthDate: '2022-07-12T03:03:18.870Z'}birthDate: "2022-07-12T03:03:18.870Z"height: nulll..

    [๋“œ๋ฆผ์ฝ”๋”ฉ] Object to JSON (stringify)

    Object to JSON stringify(obj) stringify๋Š” ๋ฐ์ดํ„ฐ๋ฅผ stringํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. let json = JSON.stringify(true); console.log(json); //"true" let json = JSON.stringify(['Noran','Paran']); console.log(json); //["Noran","Paran"] ํ•œ๊ฐœ์˜ single quote๊ฐ€ ์•„๋‹Œ double quote๋กœ ๋ฐ”๋€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด๊ฒƒ์ด JSON์˜ ๊ทœ๊ฒฉ์‚ฌํ•ญ์ด๋‹ค. ๋˜ํ•œ ์•„๋ž˜์ฒ˜๋Ÿผ ํ•จ์ˆ˜๋‚˜ javascript์—๋งŒ ์ž์ฒด์ ์œผ๋กœ ๋“ค์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐ๋„ JSON์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. const coder = { name: 'Noran', language: 'Javascript', height: nu..

    [๋“œ๋ฆผ์ฝ”๋”ฉ] JSON์ด๋ž€?

    JSON JavaScript Object Notation JSON์˜ Object ๋˜ํ•œ javascript์ฒ˜๋Ÿผ {key: value}๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ๋•Œ ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ format์ด๋‹ค. C,C++,C#,JAVA,Python,PHP ๋“ฑ์˜ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์˜ ์–ธ์–ด๋“ค์€ ๋ชจ๋‘ JSON์œผ๋กœ serialization(์ง๋ ฌํ™”)๋œ object๋ฅผ ๋‹ค์‹œ ๊ทธ ์–ธ์–ด์— ๋งž๊ฒŒ object๋กœ ๋ณ€ํ™˜ ๋ฐ ๋‹ค์‹œ JSON์œผ๋กœ serialization(์ง๋ ฌํ™”)ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ถœ์ฒ˜ : ๋“œ๋ฆผ์ฝ”๋”ฉ https://www.youtube.com/@dream-coding / MDN https://developer.mozilla.org/ko