Promise- νμ μ²λ¦¬μ 체μ΄λ(Chaining)
λΉλκΈ° μ²λ¦¬ μν κ²°κ³Όμ λ°λ₯Έ νμ μ²λ¦¬
resolve / reject μ½λ°±μ μν΄ νλ‘λ―Έμ€μ λΉλκΈ° μ²λ¦¬ μνκ° λ³ννλ©΄ (pending -> fullfilled)
νμ μ²λ¦¬λ₯Ό μνν μ½λλ₯Ό μμ±ν΄μΌ νλλ° Promise κ°μ²΄λ κ·Έλ¬ν μ½λ μμ±μ ν μ μλ λ³λμ λ©μλλ₯Ό μ 곡νλ€.
then() , catch(), finally()
*νμ μ²λ¦¬: μλ΅ κ²°κ³Ό κ° μΆμΆ or μ¬μ©
1. then(callback) - λΉλκΈ° μ²λ¦¬ μ±κ³΅ μ νΈμΆ
λΉλκΈ° μ²λ¦¬κ° μ±κ³΅νμ¬ resolve μ½λ°±μ΄ νΈμΆλμμ κ²½μ° λμνλ©°, μ€μ λμνλ κ²μ then() μ΄ μλ then(callback) ν¨μμ μΈμλ‘ μ λ¬λ μ½λ°± ν¨μμ΄λ€.
1
2
3
|
const myPromise = new Promise(executor);
myPromise.then((response) => console.log(\`μλ΅ κ²°κ³Ό: ${response}\`));
// μλ΅ κ²°κ³Ό: some data
|
cs |
2. catch(callback) - λΉλκΈ° μ²λ¦¬ μ€ν¨ μ νΈμΆ
λΉλκΈ° μ²λ¦¬κ° μ€ν¨νμ¬ reject μ½λ°±μ΄ νΈμΆλμμ κ²½μ° λμνλ ν¨μ
1
2
3
4
5
6
|
const myPromise = new Promise(executor);
myPromise
.then((response) => console.log(\`μλ΅ κ²°κ³Ό: ${response}\`))
.catch((error) => console.error(\`error: ${error}\`));
// error: μλ΅ μ€ν¨
|
cs |
3. finally(callback)
λΉλκΈ° μ²λ¦¬ μ±κ³΅/μ€ν¨ μ¬λΆμ μκ΄μμ΄ λ§μ§λ§μ ν λ²μ 무쑰건 λμνλλ‘ λ³΄μ₯λλ ν¨μ
1
2
3
4
5
6
7
8
|
const myPromise = new Promise(executor);
myPromise
.then((response) => console.log(\`μλ΅ κ²°κ³Ό: ${response}\`))
.catch((error) => console.error(\`error: ${error}\`));
.finally(() => console.log('μμ² μν μλ£'));
// μλ΅ κ²°κ³Ό: some data or error: μλ΅ μ€ν¨
// μμ² μν μλ£(무쑰건 ν λ²μ λμ)
|
cs |
νλ‘λ―Έμ€ μ²΄μ΄λ
μλμ κ°μ΄ κ°κ°μ νμ μ²λ¦¬ λ©μλλ€μ΄ μ΄μ΄μ νΈμΆν μ μλλ‘ λμνλ μ΄μ λ ν΄λΉ λ©μλλ€μ΄ Promise κ°μ²΄λ₯Ό λ°ννκΈ° λλ¬Έμ΄λ€.
1
2
3
4
5
6
7
8
|
onst myPromise = new Promise(executor);
myPromise
.then((response) => console.log(\`μλ΅ κ²°κ³Ό: ${response}\`))
.catch((error) => console.error(\`error: ${error}\`));
.finally(() => console.log('μμ² μν μλ£'));
// μλ΅ κ²°κ³Ό: some data or error: μλ΅ μ€ν¨
// μμ² μν μλ£(무쑰건 ν λ²μ λμ)
|
cs |
νλ‘λ―Έμ€ μ²΄μ΄λμ νμ©νμ¬ μ½λ°± ν¬ κ°μ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
onst url = '[https://jsonplaceholder.typicode.com'](https://jsonplaceholder.typicode.com');
const promiseGet = url => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.send();
xhr.onload = () => {
if (xhr.status === 200) {
// μλ΅ μ±κ³΅
resolve(JSON.parse(xhr.response));
} else {
// μλ¬ μ²λ¦¬
reject(new Error(xhr.status));
}
};
});
};
promiseGet(\`${url}/posts/1\`) // post idκ° 1λ²μΈ post μ‘°ν
.then((user) => promiseGet(\`${url}/users/${user.userId}\`)) // ν΄λΉ postλ₯Ό μμ±ν userμ idλ₯Ό μ΄μ©νμ¬ user μ 보 μ‘°ν
.then(userInfo => console.log(userInfo))
.catch(error => console.error(error));
|
cs |
-> then() μ ν΅ν΄ λ³λμ λ€μ¬μ°κΈ° μμ΄ μ°μμ μΌλ‘ λΉλκΈ° μ²λ¦¬ μν κ°λ₯
catch(callback)μ ν΅ν΄ μλ¬μ²λ¦¬κ° κ°λ₯νλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
const url = '[https://jsonplaceholder.typicode.com'](https://jsonplaceholder.typicode.com');
const promiseGet = url => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.send();
xhr.onload = () => {
if (xhr.status === 200) {
// μλ΅ μ±κ³΅
resolve(JSON.parse(xhr.response));
} else {
// μλ¬ μ²λ¦¬
reject(new Error(xhr.status));
}
};
});
};
promiseGet(\`${url}/xxx/1\`) // μ ν¨νμ§ μμ urlλ‘ μ‘°ν μμ²
.then((user) => console.log(user))
// .then(λ€λ₯Έ λΉλκΈ° μ²λ¦¬ μ½λ) ...
.catch(error => console.error(error));
|
cs |