[TIL] Day24 #callback #๋น„๋™๊ธฐ

Beanxxยท2022๋…„ 5์›” 28์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
24/120
post-thumbnail

[TIL] Day24
[SEB FE] Day24

โ˜‘๏ธย Callback Review

โžฐ Callback: ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ „๋‹ฌ์ธ์ž(argument)๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ํ•จ์ˆ˜
โœ‹ย ํ•„์š”์— ๋”ฐ๋ผ ์ฆ‰์‹œ ์‹คํ–‰(synchronously) ๊ฐ€๋Šฅ, ๋‚˜์ค‘์—(asynchronously) ์‹คํ–‰๋„ ๊ฐ€๋Šฅ

๐Ÿ“Žย Callback in action

  1. ๋ฐ˜๋ณต ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜ (iterator)

    // ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต ์‹คํ–‰
    [1, 2, 3].map(function(element, index) {
        return element * element;
    });
  2. ์ด๋ฒคํŠธ์— ๋”ฐ๋ฅธ ํ•จ์ˆ˜ (event handler)

    document.querySelector('#btn').addEventListener('click', function(e) {
        console.log('button clicked');
    });
    function handleClick() {
        console.log('button clicked');
    };
    // handleClick() ํ˜ธ์ถœ์‹œ return ๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” 'undefined'
    
    document.querySelector('#btn').onclick = handleClick; // โญ•๏ธ
    
    document.querySelector('#btn').onclick = function() {
        handleClick();
    } // โญ•๏ธ
    
    document.querySelector('#btn').onclick = handleClick.bind(); // โญ•๏ธ
    
    // ํ•จ์ˆ˜ ์‹คํ–‰์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹˜ -> 'ํ•จ์ˆ˜ ์ž์ฒด'๋ฅผ ์—ฐ๊ฒฐํ•ด์•ผ ํ•จ!
    document.querySelector('#btn').onclick = handleClick(); // โŒ

โ˜‘๏ธย ๋น„๋™๊ธฐ(asynchronous)

โžฐย ๋™๊ธฐ์ (synchronous): ์š”์ฒญ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋™์‹œ์— ๋ฐœ์ƒ
โžฐย blocking

โžฐย ๋น„๋™๊ธฐ์ (asynchronous): ์š”์ฒญ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ ๋™์‹œ์— ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
โžฐย non-blocking

โœ‹ย Node.js๋Š” non-blockingํ•˜๊ณ  ๋น„๋™๊ธฐ์ (asynchronous)์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ๋Ÿฐํƒ€์ž„์œผ๋กœ ๊ฐœ๋ฐœ

๐Ÿ˜ย ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ์‹คํ–‰์‹œ ์ˆœ์„œ๊ฐ€ ์ œ๋ง˜๋Œ€๋กœ โ†’ ์ˆœ์„œ ์ œ์–ด๋ฅผ ํ•˜๊ณ  ์‹ถ์–ด โ†’ callback
โ†’ But, callback ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ์“ฐ๋ฉด ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง โ‡’ callback hell
โ†’ promise ์‚ฌ์šฉ

๐Ÿ‘‰ย ์ฆ‰, ๋น„๋™๊ธฐ ํ•จ์ˆ˜์˜ ์ˆœ์„œ๋ฅผ ์ œ์–ดํ•˜๊ณ  ์‹ถ์„ ๋•Œ callback์„ ์ด์šฉ

๐Ÿ“Žย ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ์ „๋‹ฌ ํŒจํ„ด

  1. callback ํŒจํ„ด

    let request = 'coffee';
    orderCoffeeAsync(request, function(response) {
    	drink(response);
    });
  1. ์ด๋ฒคํŠธ ๋“ฑ๋ก ํŒจํ„ด

    let request = 'coffee';
    orderCoffeeAsync(request).onready = function(response) {
    	drink(response);
    });

โžฐย client: ์„œ๋ฒ„๋กœ ์ ‘์†ํ•˜๋Š” ์ปดํ“จํ„ฐ
โžฐย server: ๋ฌด์–ธ๊ฐ€(์„œ๋น„์Šค, ๋ฆฌ์†Œ์Šค ๋“ฑ)๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ปดํ“จํ„ฐ


๐Ÿ“Žย Promise

๐Ÿ˜ย callback hell ํƒˆ์ถœ์„ ์œ„ํ•ด ์‚ฌ์šฉ โ†’ But, Promise๋„ Promise hell์— ๊ฐ‡ํž ์ˆ˜ ์žˆ์Œ

  • Promise.prototype.then: Promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ๋ฉ”์„œ๋“œ ์ฒด์ด๋‹(Chaining) ๊ฐ€๋Šฅ
  • Promise.prototype.catch: ์—๋Ÿฌ๊ฐ€ ๋‚œ ๊ฒฝ์šฐ ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ (๋งˆ์ง€๋ง‰ ์ฒด์ด๋‹์—์„œ ์‹คํ–‰)
const printString = (string) => {
	// 2๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ reject์— ์—๋Ÿฌ๊ฐ€ ๋‚ฌ์„ ๊ฒฝ์šฐ์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์„ ์ ์–ด์ฃผ๋ฉด .catch์™€ ๊ฐ™์€ ์—ญํ•  ์ˆ˜ํ–‰
	return new Promise((resolve, reject) => {
		setTimeout(
			() => {
				console.log(string)
				resolve()
			},
			Math.floor(Math.random() * 100) + 1
			)
		})
	}

	const printAll = () => {
		printString('A');
		.then(() => {
			return printString('B')
		})
		.then(() => {
			return printString('C');
		})
	}
	printAll()
}

==========

function goToBed() {
	return new Promise((resolve, reject) => {
		setTimeout(() => {resolve(print goToBed)}, 100)
	})
}

๐Ÿ“Žย async / await

promise hell์„ ํƒˆ์ถœ์„ ์œ„ํ•ด ์‚ฌ์šฉ

const result = async () => {
	const one = await gotoComputer();
	console.log(one)

	const two = await sitAndCode();
	console.log(two)

	const three = await eatLunch();
	console.log(three)
}

result();

โœ‹ await ํ‚ค์›Œ๋“œ๋Š” async ํ‚ค์›Œ๋“œ๋ฅผ ์•ž์— ๋ถ™์ธ function ์•ˆ์—์„œ๋งŒ ๋™์ž‘


๐Ÿ“Žย Timer API

JS ๋‚ด์žฅ ๋น„๋™๊ธฐ ํ•จ์ˆ˜

  1. setTimeout(callback, millisecond)
    : ์ผ์ • ์‹œ๊ฐ„ ํ›„์— ํ•จ์ˆ˜ ์‹คํ–‰
    
    ```jsx
    const timer = setTimeout(function () {
      console.log('1์ดˆ ํ›„ ์‹คํ–‰');
    }, 1000);
    
    // 1000 -> 1์ดˆ
    // 10000 -> 10์ดˆ
    ```
    โœ‹ setTimeout์˜ ๋‘๋ฒˆ์งธ ์ „๋‹ฌ์ธ์ž์— ๊ฐ’์„ ๋„ฃ์ง€ ์•Š์•„๋„ ๋™์ž‘์€ ํ•˜์ง€๋งŒ, timer์˜ ๊ธฐ๋Šฅ์€ ์ œํ•œ

  1. clearTimeout(timerId)

    : setTimeout ํƒ€์ด๋จธ ์ข…๋ฃŒ
    
    ```jsx
    clearTimeout(timer);
    ```

  2. setInterval(callback, millisecond)

    : ์ผ์ • ์‹œ๊ฐ„์˜ ๊ฐ„๊ฒฉ์„ ๊ฐ€์ง€๊ณ  ํ•จ์ˆ˜ ๋ฐ˜๋ณต์  ์‹คํ–‰
    
    ```jsx
    setInterval(function() {
    	conosle.log('3์ดˆ๋งˆ๋‹ค ์‹คํ–‰');
    }, 3000);
    ```

  3. clearInterval(timerId)

    clearInterval(timer);
profile
FE developer

0๊ฐœ์˜ ๋Œ“๊ธ€