예제
1초가 걸리는 a Promise, 3초가 걸리는 b Promise, 2초가 걸리는 c Promise를 만들고 3개의 Promise를 동시에 실행하기 (비동기적 실행)
function api(promiseName, sec) {
return new Promise((resolve,reject) => {
setTimeout(() => {
if (typeof promiseName === "string" && typeof sec === 'number') {
resolve(`${promiseName} Promise 작업이 ${sec}초 걸렸습니다.`)
} else {
reject(`${promiseName} Promise 작업 실패`)
}
}, sec*1000);
})
}
const aPromise = api('a',1)
const bPromise = api('b',3)
const cPromise = api('c',2)
aPromise.then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
})
bPromise.then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
})
cPromise.then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
})
async await를 이용하여 위와 똑같은 작업 구현 (비동기적 실행)
function api(promiseName, sec) {
return new Promise((resolve,reject) => {
setTimeout(() => {
if (typeof promiseName === "string" && typeof sec === 'number') {
resolve(`${promiseName} Promise 작업이 ${sec}초 걸렸습니다.`)
} else {
reject(`${promiseName} Promise 작업 실패`)
}
}, sec*1000);
})
}
const aPromise = async function () {
try {
const response = await api("a",1)
console.log(response)
} catch (err) {
console.log(err);
}
}
const bPromise = async function () {
try {
const response = await api("b",3)
console.log(response)
} catch (err) {
console.log(err);
}
}
const cPromise = async function () {
try {
const response = await api("c",2)
console.log(response)
} catch (err) {
console.log(err);
}
}
aPromise();
bPromise();
cPromise();
sum1 Promise의 결과값을 sum5 Promise에, sum5 Promise 결과값을 multiply3 Promise에 전달하여 실행하는 경우 (동기적 실행)
function api(promiseName, sec, num) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (promiseName === "sum1") {
const response = num + 1;
console.log(`${promiseName}작업이 ${sec}초 걸렸습니다.`);
resolve(response);
} else if (promiseName === "sum5") {
const response = num + 5;
console.log(`${promiseName}작업이 ${sec}초 걸렸습니다.`);
resolve(response);
} else if (promiseName === "multiply3") {
const response = num * 3;
console.log(`${promiseName}작업이 ${sec}초 걸렸습니다.`);
resolve(response);
} else {
reject("작업 실패");
}
}, sec * 1000);
});
}
(async function () {
try {
const firstNum = 3;
console.log(`첫 입력 숫자 : ${firstNum}`);
const responseA = await api("sum1", 1, firstNum);
console.log(`현재 값 : ${responseA}`);
const responseB = await api("sum5", 3, responseA);
console.log(`현재 값 : ${responseB}`);
const responseC = await api("multiply3", 2, responseB);
console.log(`현재 값 : ${responseC}`);
} catch (err) {
console.log(err);
}
})();
Promise Methods
Promise.all
function api(promiseName, sec) {
return new Promise((resolve,reject) => {
setTimeout(() => {
if (typeof promiseName === "string" && typeof sec === 'number') {
resolve(`${promiseName} Promise 작업이 ${sec}초 걸렸습니다.`)
} else {
reject(`${promiseName} Promise 작업 실패`)
}
}, sec*1000);
})
}
const aPromise = api('a',1)
const bPromise = api('b',3)
const cPromise = api('c',2)
Promise.all([aPromise,bPromise,cPromise]).then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
})
const aPromise = api('a',1)
const bPromise = api('b','3')
const cPromise = api('c','2')
Promise.all([aPromise,bPromise,cPromise]).then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
})
Promise.allSettled
function api(promiseName, sec) {
return new Promise((resolve,reject) => {
setTimeout(() => {
if (typeof promiseName === "string" && typeof sec === 'number') {
resolve(`${promiseName} Promise 작업이 ${sec}초 걸렸습니다.`)
} else {
reject(`${promiseName} Promise 작업 실패`)
}
}, sec*1000);
})
}
const aPromise = api('a',1)
const bPromise = api('b','3')
const cPromise = api('c','2')
Promise.allSettled([aPromise,bPromise,cPromise]).then((res) => {
console.log(res);
})
Promise.race
function api(promiseName, sec) {
return new Promise((resolve,reject) => {
setTimeout(() => {
if (typeof promiseName === "string" && typeof sec === 'number') {
resolve(`${promiseName} Promise 작업이 ${sec}초 걸렸습니다.`)
} else {
reject(`${promiseName} Promise 작업 실패`)
}
}, sec*1000);
})
}
const aPromise = api('a',1)
const bPromise = api('b','3')
const cPromise = api('c','2')
Promise.race([aPromise,bPromise,cPromise]).then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
})
Promise.any
function api(promiseName, sec) {
return new Promise((resolve,reject) => {
setTimeout(() => {
if (typeof promiseName === "string" && typeof sec === 'number') {
resolve(`${promiseName} Promise 작업이 ${sec}초 걸렸습니다.`)
} else {
reject(`${promiseName} Promise 작업 실패`)
}
}, sec*1000);
})
}
const aPromise = api('a','1')
const bPromise = api('b',3)
const cPromise = api('c',2)
Promise.any([aPromise,bPromise,cPromise]).then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
})