Baseline Widely available
Promise.race()
ë©ìëë Promise ê°ì²´ë¥¼ ë°íí©ëë¤. ì´ íë¡ë¯¸ì¤ ê°ì²´ë iterable ìì ìë íë¡ë¯¸ì¤ ì¤ì ê°ì¥ 먼ì ìë£ë ê²ì ê²°ê³¼ê°ì¼ë¡ ê·¸ëë¡ ì´ííê±°ë ê±°ë¶í©ëë¤.
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 500, "one");
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, "two");
});
Promise.race([promise1, promise2]).then((value) => {
console.log(value);
// Both resolve, but promise2 is faster
});
// Expected output: "two"
구문 매ê°ë³ì
ë°íê°
주ì´ì§ iterableìì ì²ìì¼ë¡ ì´ííê±°ë ê±°ë¶í íë¡ë¯¸ì¤ì ê°ì ë¹ë기ì ì¼ë¡ ì ë¬ë°ë ë기 ì¤ì¸ Promise
.
race
í¨ìë ì¸ìë¡ ì£¼ì´ì§ iterableì íë¡ë¯¸ì¤ ì¤ ê°ì¥ 먼ì ìë£(settle)ëë ê²ê³¼ ê°ì ë°©ìì¼ë¡ ìë£(ì´í/ê±°ë¶)ëê³ , ê°ì ê²°ê³¼ê°ì ì ë¬íë Promise
를 ë°íí©ëë¤.
ì ë¬ë°ì iterableì´ ë¹ì´ ìì ê²½ì°, ë°íí íë¡ë¯¸ì¤ë ììí ë기 ìíê° ë©ëë¤.
Iterableì íë¡ë¯¸ì¤ê° ìë ê°ì´ë ì´ë¯¸ ìë£ë íë¡ë¯¸ì¤ê° í¬í¨ëì´ ìì ê²½ì°, Promise.race
ë ì ë¬ë°ì iterableìì ì²ìì¼ë¡ ë±ì¥íë ì´ë¬í ê°ì ê²°ê³¼ê°ì¼ë¡ ì´íí©ëë¤.
Promise.race
ì ë¹ë기ì±
ë¤ì ìì ìì Promise.race
ì ë¹ë기ì±ì íì¸í ì ììµëë¤.
// Promise.race를 ìµëí 빨리 ìë£ìí¤ê¸° ìí´
// ì´ë¯¸ ì´íë íë¡ë¯¸ì¤ë¡ ë°°ì´ì ë§ë¤ì´ ì¸ìë¡ ì ë¬
var resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)];
var p = Promise.race(resolvedPromisesArray);
// ì¤í ì¦ì pì ê°ì 기ë¡
console.log(p);
// í¸ì¶ ì¤íì ë¹ì´ ë¤ì ì¤íí기 ìí´ setTimeoutì ì¬ì©
setTimeout(function () {
console.log("the stack is now empty");
console.log(p);
});
// ë¡ê·¸ ì¶ë ¥ ê²°ê³¼ (ììëë¡):
// Promise { <state>: "pending" }
// the stack is now empty
// Promise { <state>: "fulfilled", <value>: 33 }
ë¹ì´ ìë iterableì ì ë¬íë©´ ë°íí íë¡ë¯¸ì¤ë ììí ë기 ìíê° ë©ëë¤.
var foreverPendingPromise = Promise.race([]);
console.log(foreverPendingPromise);
setTimeout(function () {
console.log("the stack is now empty");
console.log(foreverPendingPromise);
});
// ë¡ê·¸ ì¶ë ¥ ê²°ê³¼ (ììëë¡):
// Promise { <state>: "pending" }
// the stack is now empty
// Promise { <state>: "pending" }
Iterableì íë¡ë¯¸ì¤ê° ìë ê°ì´ë ì´ë¯¸ ìë£ë íë¡ë¯¸ì¤ê° í¬í¨ëì´ ìì ê²½ì°, Promise.race
ë ì ë¬ë°ì iterableìì ì²ìì¼ë¡ ë±ì¥íë ì´ë¬í ê°ì ê²°ê³¼ê°ì¼ë¡ ì´íí©ëë¤.
var foreverPendingPromise = Promise.race([]);
var alreadyFulfilledProm = Promise.resolve(666);
var arr = [foreverPendingPromise, alreadyFulfilledProm, "íë¡ë¯¸ì¤ ìë"];
var arr2 = [foreverPendingPromise, "íë¡ë¯¸ì¤ ìë", Promise.resolve(666)];
var p = Promise.race(arr);
var p2 = Promise.race(arr2);
console.log(p);
console.log(p2);
setTimeout(function () {
console.log("the stack is now empty");
console.log(p);
console.log(p2);
});
// ë¡ê·¸ ì¶ë ¥ ê²°ê³¼ (ììëë¡):
// Promise { <state>: "pending" }
// Promise { <state>: "pending" }
// the stack is now empty
// Promise { <state>: "fulfilled", <value>: 666 }
// Promise { <state>: "fulfilled", <value>: "íë¡ë¯¸ì¤ ìë" }
setTimeout
ê³¼ í¨ê» Promise.race
ì¬ì© ì
var p1 = new Promise(function (resolve, reject) {
setTimeout(() => resolve("íë"), 500);
});
var p2 = new Promise(function (resolve, reject) {
setTimeout(() => resolve("ë"), 100);
});
Promise.race([p1, p2]).then(function (value) {
console.log(value); // "ë"
// ë ë¤ ì´ííì§ë§ p2ê° ë ë¹ ë¥´ë¯ë¡
});
var p3 = new Promise(function (resolve, reject) {
setTimeout(() => resolve("ì
"), 100);
});
var p4 = new Promise(function (resolve, reject) {
setTimeout(() => reject(new Error("ë·")), 500);
});
Promise.race([p3, p4]).then(
function (value) {
console.log(value); // "ì
"
// p3ì´ ë ë¹ ë¥´ë¯ë¡ ì´íí¨
},
function (reason) {
// ì¤íëì§ ìì
},
);
var p5 = new Promise(function (resolve, reject) {
setTimeout(() => resolve("ë¤ì¯"), 500);
});
var p6 = new Promise(function (resolve, reject) {
setTimeout(() => reject(new Error("ì¬ì¯")), 100);
});
Promise.race([p5, p6]).then(
function (value) {
// ì¤íëì§ ìì
},
function (error) {
console.log(error.message); // "ì¬ì¯"
// p6ì´ ë ë¹ ë¥´ë¯ë¡ ê±°ë¶í¨
},
);
ëª
ì¸ ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4