λΉλκΈ° μμ μ μ΅μ’ μ±κ³΅ λλ μ€ν¨λ₯Ό λνλ΄λ κ°μ²΄
- μ΄ 3κ°μ§ μνλ₯Ό κ°λλ€.
Pending
(λκΈ°): λΉλκΈ° μμ μ΄ μμ§ μλ£λμ§ μμ μνFulfilled
(μ΄ν): λΉλκΈ° μμ μ΄ μ±κ³΅μ μΌλ‘ μλ£λμ΄, λΉλκΈ° μμ μ κ²°κ³Όλ¬Όμ΄ κ°μ²΄μ λ€μ΄μλ€.Rejected
(μ€ν¨): λΉλκΈ° μμ μ΄ μ€ν¨ν μνλ‘, μλ¬λ©μμ§κ° λ€μ΄μλ€.
const promise = new Promise(function(resolve, reject){...});
const promise = new Promise((resolve, reject) => {...});
reslove
μ reject
λΌλ 2κ°μ ν¨μν νλΌλ―Έν°λ₯Ό κ°μ§λ€.resolve()
λ reject()
ν¨μλ₯Ό μ±κ³΅, μ€ν¨ μ¬λΆμ λ°λΌ μ μ ν νΈμΆν΄μ€μΌ νλ€.then()
λ©μλμ catch()
λ©μλκ° μλ€.then()
λ©μλ: κ²°κ³Όκ°μ κ°μ§κ³ μνν λ‘μ§μ λ΄μ μ½λ°± ν¨μλ₯Ό μΈμλ‘ λ°λλ€.catch()
λ©μλ: μμΈ μ²λ¦¬ λ‘μ§μ λ΄μ μ½λ°± ν¨μλ₯Ό μΈμλ‘ λ°λλ€.fetch()
ν¨μλ APIμ URLμ μΈμλ‘ λ°κ³ , API νΈμΆ κ²°κ³Όλ₯Ό Promise κ°μ²΄λ‘ 리ν΄νλ€.fetch("https://jsonplaceholder.typicode.com/posts/1")
.then((response) => console.log("response:", response))
.catch((error) => console.log("error:", error));
fetch()
.then((response) => console.log("response:", response))
.catch((error) => console.log("error:", error));
then()
κ³Ό catch()
λ©μλλ₯Ό ν΅ν΄ λκΈ°μ μ½λμμ μ¬μ©νλ try-catch
λΈλ‘κ³Ό μ μ¬ν λ°©λ²μΌλ‘ λΉλκΈ° μ½λλ₯Ό μμ±ν μ μλ€.then()
κ³Ό catch()
λ©μλλ λ λ€λ₯Έ Promise κ°μ²΄λ₯Ό λ°ννλ€. then()
κ³Ό catch()
λ©μλλ₯Ό ν΅ν΄ λ€μ μ κ·Όν μ μλλ‘ ν΄μ€λ€.then()
κ³Ό catch()
λ©μλλ μ°μμ μΌλ‘ νΈμΆν μ μλ€.fetch("https://jsonplaceholder.typicode.com/posts/1")
.then((response) => response.json())
.then((post) => post.userId)
.then((userId) => "https://jsonplaceholder.typicode.com/users/" + userId)
.then((url) => fetch(url))
.then((response) => response.json())
.then((user) => console.log("user:", user))
.catch((error) => console.log("error:", error));
const promise = new Promise((resolve, reject) =>{
resolve();
});
promise
.then(data => {
throw new Error("opps");
console.log("first value");
})
.catch(() => {
console.log("catched an error");
})
.then(data => {
console.log("second value");
});
// catched an error
// second value
Promise.resolve()
μ Promise.reject()
λ μλμΌλ‘(μ¦μ) μ±κ³΅νκ±°λ μ€ν¨νλ νλ‘λ―Έμ€λ₯Ό μμ±νλ€.Promise.resolve()
λ μ¦μ νλ‘λ―Έμ€λ₯Ό μ±κ³΅ μ²λ¦¬νλ―λ‘ μ²« λ²μ§Έ ν¨μκ° νΈμΆλλ€.Promise.resolve("Success").then(function(value){
console.log(value);
}, function(value){
console.log("fail");
});
// Success
Promise.reject()
λ νλ‘λ―Έμ€λ₯Ό μ¦μ μ€ν¨ μ²λ¦¬νλ―λ‘ then()
μ λ λ²μ§Έ μΈμκ° νΈμΆλλ€.Promise.reject(new Error("fail")).then(function(){
// not called
}, function(error){
console.log(error);
});
// Error: fail
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, 'first value');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 'second value');
});
promise1.then(data => {
console.log(data);
});
// 500ms ν first value
promise2.then(data => {
console.log(data);
});
// 1000ms ν second value
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, 'first value');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 'second value');
});
Promise
.all([promise1, promise2])
.then(data => {
const [promise1data, promise2data] = data;
console.log(promise1data, promise2data);
});
// 1000ms ν
// first value second value
const promise1 = new Promise((resolve, reject) => {
resolve("my first value");
});
const promise2 = new Promise((resolve, reject) => {
reject(Error("opps error"));
});
Promise
.all([promise1, promise2])
.then(data => {
const [promise1data, promise2data] = data;
console.log(promise1data, promise2data);
});
.catch(err => {
console.log(err);
}
// Error: opps error
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, 'first value');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'second value');
});
Promise.race([promise1, promise2]).then(function(value) {
console.log(value);
});
// second value
μ°Έκ³ : [μλ°μ€ν¬λ¦½νΈ] λΉλκΈ° μ²λ¦¬ 2λΆ - Promise