📚 Reference


📜 Chapter


Promise.race()


예시


var p1 = new Promise((resolve, reject) => {
  setTimeout(() => resolve('하나'), 1000);
});
var p2 = new Promise((resolve, reject) => {
  setTimeout(() => resolve('둘'), 2000);
});
var p3 = new Promise((resolve, reject) => {
  setTimeout(() => resolve('셋'), 3000);
});
var p4 = new Promise((resolve, reject) => {
  setTimeout(() => resolve('넷'), 4000);
});

Promise.race([p1, p2, p3, p4, p5])
.then(value => {
  console.log(value);
});

// 콘솔 출력값:
// "하나"
var p1 = new Promise((resolve, reject) => {
  setTimeout(() => reject('하나'), 1000);
});
var p2 = new Promise((resolve, reject) => {
  setTimeout(() => reject('둘'), 2000);
});
var p3 = new Promise((resolve, reject) => {
  setTimeout(() => reject('셋'), 3000);
});
var p4 = new Promise((resolve, reject) => {
  setTimeout(() => reject('넷'), 4000);
});

Promise.race([p1, p2, p3, p4, p5])
.then(value => {
  console.log(value);
})
.catch(error=>{
  console.log("error",error);
});

// 콘솔 출력값:
// error 하나