Quick Start Typescript ~ 12์žฅ ์ •๋ฆฌ

์นดํƒˆ๋กœ๊ทธ
  1. 1. 12์žฅ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ
    1. 1.1. ๐Ÿ“395p. ๋ธŒ๋ผ์šฐ์ € Promise ์ง€์› ํ˜„ํ™ฉ
    2. 1.2. ๐Ÿ“SetTimeout 3๋ฒˆ์งธ ์ธ์ž
    3. 1.3. Promise.all([]) ๊ฒฐ๊ณผ๊ฐ’
    4. 1.4. ๐Ÿ“404p. RxJS
      1. 1.4.1. ์„ค์น˜
      2. 1.4.2. Import
      3. 1.4.3. ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆผ์˜ ํ˜•ํƒœ
      4. 1.4.4. ๋น„๋™๊ธฐ ์ฝ”๋“œ ์ œ์–ด์˜ ๋ฐฉ์‹ ์ฐจ์ด

๐Ÿ“– Quick Start Typescript ์„ ์ฝ๊ณ , ๊ฐ„๋‹จํžˆ ๋ชฐ๋ž๋˜ ๋ถ€๋ถ„์ด๋‚˜ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์ž‘์„ฑ

12์žฅ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ

๐Ÿ“395p. ๋ธŒ๋ผ์šฐ์ € Promise ์ง€์› ํ˜„ํ™ฉ

IE ์ง€์› ์•ˆํ•จ

Chrome 57 ์ด์ƒ

Safari 10 ์ด์ƒ

Android Browser 53 ์ด์ƒ

IOS safari 10.2 ์ด์ƒ

๐Ÿ“SetTimeout 3๋ฒˆ์งธ ์ธ์ž

https://developer.mozilla.org/ko/docs/Web/API/WindowTimers/setTimeout

image-20201020004247332

param ~ paramN : ํƒ€์ด๋จธ ์™„๋ฃŒ ์ดํ›„, ์ถ”๊ฐ€์ ์œผ๋กœ ์ „๋‹ฌ๋˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ

1
2
3
4
5
6
7
8
9
10
const promiseAsync = new Promise((resolve, reject) => {
let sec = Math.floor(Math.random() * 5) + 1;
setTimeout((isTrue: boolean) => {
if (isTrue) {
resolve(sec);
}
}, sec * 1000, true);
}).then(res => {
console.log(res + `s`);
});

Promise.all([]) ๊ฒฐ๊ณผ๊ฐ’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function asyncDelay(order: number) {
return new Promise((resolve) => {
const ms = Math.floor(Math.random() * 1000) + 1;

window.setTimeout(() => {
console.log(`์ž‘์—… ์™„๋ฃŒ: ` + order);
resolve(order);
}, ms);
});
}

function syncResultPromise() {
const p1 = asyncDelay(1);
const p2 = asyncDelay(2);
const p3 = asyncDelay(3);
const p4 = asyncDelay(4);

Promise.all([p1, p2, p3, p4]).then(order => {
console.log(`๋™๊ธฐํ™”๋œ ์ถœ๋ ฅ: ${order}`);
}); // LOG: ๋™๊ธฐํ™”๋œ ์ถœ๋ ฅ: 1,2,3,4
}

syncResultPromise();

image-20201020005554668

[p1, p2, p3, p4]์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ํ•œ๋ฒˆ์— ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Œ

๐Ÿ“404p. RxJS

RxJS์— ๊ด€ํ•œ ์ฑ…์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•˜๊ณ  ๋„˜์–ด๊ฐ„ ๋“ฏ ํ•˜๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ๋งŒ ์ •๋ฆฌ

๋ฐ˜์‘ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ชจ๋ธ์€ ์ŠคํŠธ๋ฆผ ํ˜•ํƒœ์˜ ์ž…๋ ฅ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•ด ๋ฐ˜์‘์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋ธ

์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆผ์€ ๊ด€์ธกํ•  ์ˆ˜ ์žˆ๊ณ  ๋‹ค๋ž„ ์ˆ˜ ์žˆ๋Š” ๋Œ€์ƒ์ด ๋˜๋ฏ€๋กœ observables์ด ๋จ

๋ฐœํ–‰ ๊ตฌ๋… ํŒจํ„ด

์˜ต์ €๋ฒ„ <-> ์˜ต์ €๋ฒ„๋ธ”

-> ๊ตฌ๋…

<- ํ†ต์ง€

์„ค์น˜

1
npm i @reactivex/rxjs

Import

1
import * as Rx from '@reactivex/rxjs';

๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆผ์˜ ํ˜•ํƒœ

์œ ํ˜•1: ๊ทœ์น™์ด ์žˆ๊ณ , ์ œํ•œ๋ฒ”์œ„๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ

์œ ํ˜•2: ๊ทœ์น™์ด ์žˆ๊ณ , ์ œํ•œ ๋ฒ”์œ„๊ฐ€ ์—†๋Š”

์œ ํ˜•3: ๊ทœ์น™์ด ์—†๊ณ , ์ œํ•œ ๋ฒ”์œ„๊ฐ€ ์žˆ๋Š”

์œ ํ˜•4: ๊ทœ์น™์ด ์—†๊ณ , ์ œํ•œ ๋ฒ”์œ„๊ฐ€ ์—†๋Š”

๋น„๋™๊ธฐ ์ฝ”๋“œ ์ œ์–ด์˜ ๋ฐฉ์‹ ์ฐจ์ด

promise์™€ ๋‹ค๋ฅด๊ฒŒ ์˜คํผ๋ ˆ์ดํ„ฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฐ์‡„์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๊ด€๋ จ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณต

  1. ์ƒ์„ฑ ์—ฐ์‚ฐ์ž

  2. ๋ณ€ํ˜• ์—ฐ์‚ฐ์ž

  3. ์ฝค๋น„๋„ค์ด์…˜ ์—ฐ์‚ฐ์ž

  4. ์กฐ๊ฑด ์—ฐ์‚ฐ์ž

  5. ํ•„ํ„ฐ๋ง ์—ฐ์‚ฐ์ž

์ž์„ธํ•œ ์‚ฌํ•ญ์€ RxJs์— ๊ด€ํ•œ ๋ฌธ์„œ๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žต

์ถ”ํ›„, ๋ณ„๋„ ๋ฌธ์„œ์—์„œ ๊ณต์‹ ๋„ํ๋จผํŠธ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•˜๊ธฐ๋กœโ€ฆ