Graceful asynchronous programming with Promises
Promises ë ì´ì ìì ì´ ìë£ë ë ê¹ì§ ë¤ì ìì ì ì°ê¸° ìí¤ê±°ë, ìì ì¤í¨ë¥¼ ëìí ì ìë ë¹êµì ìë¡ì´ JavaScript 기ë¥ì ëë¤. Promiseë ë¹ë기 ìì ììê° ì ííê² ìëëê² ëìì ì¤ëë¤. ì´ë² 문ììì Promiseê° ì´ë»ê² ëìíëì§, ì¹ APIì ì´ë»ê² ì¬ì©í ì ìëì§ ê·¸ë¦¬ê³ ì§ì ì½ë를 ë§ë¤ì´ ë³¼ê² ì ëë¤.
| Prerequisites: | Basic computer literacy, a reasonable understanding of JavaScript fundamentals. |
|---|---|
| Objective: | To understand promises and how to use them. |
What are promises?
ììì Promises 를 미리 ë´¤ì§ë§, ì§ê¸ë¶í´ ì¢ë ê¹ì´ìê² ë¤ì¬ë¤ ë³¼ ì°¨ë¡ ì ëë¤..
Promiseë ì´ë¤ ìì ì ì¤ê°ìí를 ëíë´ë ì¤ë¸ì í¸ ì ëë¤. â 미ëì ì´ë¤ ì¢ ë¥ì ê²°ê³¼ê° ë°íë¨ì promise (ì½ì) í´ì£¼ë ì¤ë¸ì í¸ë¼ê³ ë³´ë©´ ë©ëë¤. Promiseë ìì ì´ ìë£ëì´ ê²°ê³¼ë¥¼ ë°íí´ì£¼ë ì íí ìê°ì ë³´ì¥í´ì£¼ì§ë ìì§ë§, ì¬ì©í ì ìë 결과를 ë°ííìë íë¡ê·¸ë머ì ìëëë¡ ë¤ì ì½ë를 ì§í ìí¤ê±°ë, ìë¬ê° ë°ìíì ë ê·¸ ìë¬ë¥¼ ì°ìíê²/ê¹ëíê² ì²ë¦¬í ì ììµëë¤.
ì¼ë°ì ì¼ë¡ ì°ë¦¬ë ë¹ë기 ìì ì´ ê²°ê³¼ë¥¼ ë°ííëë° ì¼ë§ì ìê°ì´ 걸리ëì§ ë³´ë¤ë(ìì ìê°ì´ ë§¤ì° ì¤ë ê±¸ë¦¬ì§ ìë í) ê·¸ 결과를 ì¬ì©í ì ìëì§ ì¬ë¶ì ë ê´ì¬ì´ ììµëë¤. ë¬¼ë¡ ëë¨¸ì§ ì½ë ë¸ëì ë§ì§ ìëë¤ë ê²ì ìì´ì ë§¤ì° ì¢ìµëë¤.
ì°ë¦¬ê° Promiseë¡ ê°ì¥ ë§ì´ í ìì ì¤ íëë Promise를 ë°ííë ì¹API를 ì¬ì©íë ê² ì ëë¤. ê°ìì ë¹ëì¤ ì±í ì í리ì¼ì´ì ì´ ìë¤ê³ í´ë´ ìë¤. ì í리ì¼ì´ì ìë ì¹êµ¬ 목ë¡ì´ ìê³ ê° ì¹êµ¬ ëª©ë¡ ìì ë²í¼ì í´ë¦íë©´ í´ë¹ ì¹êµ¬ì ë¹ëì¤ ì±í ì ììí©ëë¤.
ê·¸ ë²í¼ì ì¬ì©ì ì»´í¨í°ì ì¹´ë©ë¼ì ë§ì´í¬ë¥¼ ì¬ì©í기 ìí´ getUserMedia() 를 í¸ì¶í©ëë¤. getUserMedia() ë ì¬ì©ìê° ì´ë¬í ì¥ì¹ë¥¼ ì¬ì©í ì ìë ê¶íì ê°ì§ê³ ìëì§ íì¸í´ì¼ íê³ , ì´ë¤ ë§ì´í¬ì ì¹´ë©ë¼ë¥¼ ì¬ì©í ê²ì¸ì§ (í¹ì ìì± íµíì¸ì§, ìëë©´ ë¤ë¥¸ ìµì
ë¤ì´ ìëì§)를 ì²´í¬í´ì¼í기 ë문ì 모ë ê²°ì ì´ ë´ë ¤ì§ ë ê¹ì§ ë¤ì ìì
ì ì°¨ë¨í ì ììµëë¤. ëí ì¹´ë©ë¼ì ë§ì´í¬ê° ìëí기 ì ê¹ì§ ë¤ì ìì
ì ì°¨ë¨í ìë ììµëë¤.
getUserMedia() ë ë¸ë¼ì°ì ì main threadìì ì¤íëë¯ë¡ getUserMedia() ê²°ê³¼ê° ë°íë기 ì ê¹ì§ íí ìì
ì´ ëª¨ë ì°¨ë¨ë©ëë¤. ì´ë¬í blockingì ì°ë¦¬ê° ë°ë¼ëê² ìëëë¤. Promiseê° ìì¼ë©´ ì´ë¬í ê²°ì ì´ ë´ë ¤ì§ê¸° ì ê¹ì§ ë¸ë¼ì°ì ì 모ë ê²ì ì¬ì©í ì ìê²ë©ëë¤. ë°ë¼ì ì¬ì©ìê° ì íí ì¥ì¹ë¥¼ íì±ííê³ ìì¤ìì ì íë ì¤í¸ë¦¼ì ëí´MediaStream ì§ì ë°ííë ëì getUserMedia() ë 모ë ì¥ì¹ê° ì¬ì© ê°ë¥í ìíê° ëë©´ MediaStreamì´ í¬í¨ë Promise를 ë°íí©ëë¤.
ë¹ëì¤ ì±í ì í리ì¼ì´ì ì ì½ëë ìëì²ë¼ ìì±í ì ììµëë¤.
function handleCallButton(evt) {
setStatusMessage("Calling...");
navigator.mediaDevices
.getUserMedia({ video: true, audio: true })
.then((chatStream) => {
selfViewElem.srcObject = chatStream;
chatStream
.getTracks()
.forEach((track) => myPeerConnection.addTrack(track, chatStream));
setStatusMessage("Connected");
})
.catch((err) => {
setStatusMessage("Failed to connect");
});
}
ì´ ê¸°ë¥ì ìí ë©ìì§ì "Calling..."ì ì¶ë ¥íë setStatusMessage() í¨ìë¡ ììíë©° íµíê° ìëëê³ ììì ëíë
ëë¤. ê·¸ë° ë¤ì getUserMedia()ì í¸ì¶íì¬ ë¹ëì¤ì ì¤ëì¤ í¸ëì´ ëª¨ë ìë ì¤í¸ë¦¼ ìì²ì í©ëë¤. ê·¸ë¦¬ê³ ì¤í¸ë¦¼ì íëíë©´ ì¹´ë©ë¼ìì ëì¤ë ì¤í¸ë¦¼ì "self view,"ë¡ íìí기 ìí´ videoì리먼í¸ë¥¼ ì¤ì í©ëë¤. ê·¸ë¦¬ê³ ê° ì¤í¸ë¦¼ì í¸ëì ê°ì ¸ê° ë¤ë¥¸ ì¬ì©ììì ì°ê²°ì ëíë´ë WebRTC RTCPeerConnectionì ì¶ê°í©ëë¤. ê·¸ë¦¬ê³ ë§ì§ë§ì¼ë¡ ìí ë©ìì§ë¥¼ "Connected"ë¡ ì
ë°ì´í¸ í©ëë¤.
getUserMedia() ê° ì¤í¨íë©´, catch ë¸ëì´ ì¤íëë©°, setStatusMessage() 를 ì¬ì©íì¬ ìíì°½ì ì¤ë¥ ë©ìì§ë¥¼ íìí©ëë¤.
ì¬ê¸°ì ì¤ìíê±´ getUserMedia()ë ì¹´ë©ë¼ ì¤í¸ë¦¼ì´ ìì§ íë³´ëì§ ììììë ê±°ì ì¦ì ë°íì í´ì¤¬ë¤ë ê² ì
ëë¤. handleCallButton() í¨ìê° ìì ì í¸ì¶í ì½ëë¡ ê²°ê³¼ë¥¼ ì´ë¯¸ ë°íì íëë¼ë getUserMedia()ì ìì
ì´ ì¢
ë£ëë©´ íë¡ê·¸ëë¨¸ê° ìì±í ë¤ì í¸ë¤ë¬ë¥¼ í¸ì¶í ê² ì
ëë¤. ì±ì´ ì¤í¸ë¦¬ë°ì íë¤ê³ ê°ì íì§ ìë í ê³ì ì¤íë ì ììµëë¤.
ì°¸ê³ : You can learn more about this somewhat advanced topic, if you're interested, in the article Signaling and video calling. Code similar to this, but much more complete, is used in that example.
The trouble with callbacks
Promiseê° ì ì¢ìì§ ì´í´í기 ìí´ êµ¬ì callbacksì ì´í´ë³´ê³ ì´ë¤ê² 문ì ì¸ì§ íì í´ë³´ê² ìµëë¤.
í¼ì를 주문íë¤ê³ ìê°í´ë´ ìë¤. í¼ì를 ì 주문íë ¤ë©´ ëª ê°ì§ ë¨ê³ë¥¼ ì§íí´ì¼ í©ëë¤. í í ìì ëì°ë¥¼ ì¬ë¦¬ê³ ì¹ì¦ë¥¼ ë¿ë¦¬ë ë± ê° ë¨ê³ê° ë¤ì£½ë°ì£½ ì´ê±°ë í¹ì ëì°ë¥¼ ë°ì£½íê³ ìëë° ê·¸ ìì í ë§í ìì¤ë¥¼ ë°ë¥´ë ë± ì´ì ìì ì´ ëëì§ ìê³ ë¤ì ìì ì ì§ííë ê²ì ë§ì´ ì ë©ëë¤. :
- 먼ì ìíë í íì ê³ ë¦ ëë¤. ê²°ì ì¥ì ê° ì¬í ê²½ì° í íì ê³ ë¥´ëë° ì¤ë 걸릴 ì ììµëë¤. ëí ë§ìì ë°ê¿ í¼ì ëì ì¹´ë 를 먹ì¼ë ¤ê³ ê°ê²ë¥¼ ëì¬ ì ììµëë¤.
- ê·¸ë¤ì í¼ì를 주문í©ëë¤. ìë¹ì´ ë°ë¹ ì í¼ìê° ëì¤ë ë° ì¤ë 걸릴 ì ìê³ , ë§ì¹¨ ì¬ë£ê° ë¤ ë¨ì´ì¡ì¼ë©´ í¼ì를 ë§ë¤ ì ìë¤ê³ í ê² ì ëë¤.
- ë§ì§ë§ì¼ë¡ í¼ì를 ë°ìì 먹ìµëë¤. ê·¸ë°ë°! ë§ì½ ì§ê°ì ëê³ ìì ëì ë´ì§ 못íë¤ë©´ í¼ì를 ë¨¹ì§ ëª»í ì ììµëë¤.
구ì callbacksì ì¬ì©íë©´ ìëì ê°ì 모ìµì ì½ëê° ëíë ê² ì ëë¤. :
chooseToppings(function (toppings) {
placeOrder(
toppings,
function (order) {
collectOrder(
order,
function (pizza) {
eatPizza(pizza);
},
failureCallback,
);
},
failureCallback,
);
}, failureCallback);
ì´ë° ì½ëë ì½ê¸°ë íë¤ ë¿ ìëë¼ (ì¢
ì¢
"ì½ë°± ì§ì¥" ì´ë¼ ë¶ë¦¼), failureCallback() ì ì¬ë¬ ë² ìì±í´ì¼ íë©° ëí ë¤ë¥¸ 문ì ì ë ê°ì§ê³ ììµëë¤.
Improvements with promises
ìì ìí©ìì Promise를 ì¬ì©íë©´ ì½ê¸°, ìì±, ì¤í 모ë ë¤ ì¬ìì§ëë¤. callback ëì ë¹ë기 Promise를 ì¬ì©íë©´ ìëì²ë¼ ìì±í ì ììµëë¤. :
chooseToppings()
.then(function (toppings) {
return placeOrder(toppings);
})
.then(function (order) {
return collectOrder(order);
})
.then(function (pizza) {
eatPizza(pizza);
})
.catch(failureCallback);
보기ì í¨ì¬ ë ì¢êµ°ì! â ì´ë ê² ìì±íë©´ ìì¼ë¡ ì´ë¤ ì¼ì´ ì¼ì´ë ì§ ì½ê² ì측 ê°ë¥í©ëë¤. ê·¸ë¦¬ê³ ë¨ íê°ì .catch() ì ì¬ì©íì¬ ëª¨ë ìë¬ë¥¼ ì²ë¦¬í©ëë¤. ê·¸ë¦¬ê³ main thread를 ì°¨ë¨íì§ ììµëë¤. (ê·¸ëì í¼ì를 주문íê³ ê¸°ë¤ë¦¬ë ëì íë ê²ìì ë§ì í ì ììµëë¤.), ëí ê° í¨ìê° ì¤íë기 ì ì´ì ìì
ì´ ëë ëê¹ì§ 기ë¤ë¦½ëë¤. ì´ë°ìì¼ë¡ ì¬ë¬ ê°ì ë¹ë기 ìì
ì ì°ìì ì¼ë¡ ì²ë¦¬í ì ììµëë¤. ìëíë©´ ê° .then() ë¸ëì ìì ì´ ìí ë¸ëì ìì
ì´ ëë¬ì ëì 결과를 ìë¡ì´ Promise ë°íí´ì£¼ê¸° ë문ì
ëë¤. ì´ëì, ì°¸ ì½ì£ ?
íì´í í¨ì를 ì¬ì©íë©´ ì½ë를 ì¡°ê¸ ë ê°ë¨íê² ê³ ì¹ ì ììµëë¤. :
chooseToppings()
.then((toppings) => {
return placeOrder(toppings);
})
.then((order) => {
return collectOrder(order);
})
.then((pizza) => {
return eatPizza(pizza);
})
.catch(failureCallback);
í¹ì ìëì²ë¼ ííí ì ììµëë¤. :
chooseToppings()
.then((toppings) => placeOrder(toppings))
.then((order) => collectOrder(order))
.then((pizza) => eatPizza(pizza))
.catch(failureCallback);
íì´í í¨ìì () => x ííì () => { return x; }ì ì½ì ííì´ë¯ë¡ ì ìëí©ëë¤.
í¨ìë arguments를 ì§ì ì ë¬ íë¯ë¡ í¨ìì²ë¼ íííì§ ìê³ ìëì ê°ì´ ìì±í ìë ììµëë¤. :
chooseToppings()
.then(placeOrder)
.then(collectOrder)
.then(eatPizza)
.catch(failureCallback);
ê·¸ë°ë° ì´ë ê² ìì±íë©´ ì½ê¸°ê° ì½ì§ ììµëë¤. ì¬ì©ìì ì½ëê° ì§ê¸ì ìì ë³´ë¤ ë ë³µì¡íë¤ë©´ ìì ë°©ë²ì ì¬ì©í기 íëëë¤.
ì°¸ê³ :
ë¤ì ì¥ìì ë°°ì¸ async/await 문ë²ì¼ë¡ ì¢ ë ê°ê²°í í ì ììµëë¤.
Promiseë ì´ë²¤í¸ 리ì¤ëì ì ì¬íì§ë§ ëª ê°ì§ ë¤ë¥¸ì ì´ ììµëë¤. :
- Promiseë íë²ì ì±ê³µ/ì¤í¨ ì¤ íëì ê²°ê³¼ê°ì ê°ì§ëë¤. íëì ìì²ì ë ë² ì±ê³µíê³ ë ì¤í¨í ì ììµëë¤. ëí ì´ë¯¸ ì±ê³µí ìì ì´ ë¤ì ì¤í¨ë¡ ëìê° ì ìê³ ì¤í¨í ìì ì´ ì±ê³µì¼ë¡ ëìê° ì ììµëë¤.
- If a promise has succeeded or failed and you later add a success/failure callback, the correct callback will be called, even though the event took place earlier.
Explaining basic promise syntax: A real example
모ë ì¹ APIë ì ì¬ì ì¼ë¡ 긴 ìì ì ìííë í¨ìì Promise를 ì¬ì©íë¯ë¡ Promiseê° ë¬´ìì¸ì§ ì´í´íëê²ì ë§¤ì° ì¤ìí©ëë¤. íëì ì¸ ì¹ ê¸°ì ì ì¬ì©íë ¤ë©´ Promise를 ì¬ì©í´ì¼í©ëë¤. ì±í°ì íë°ë¶ìì ì§ì Promise를 ë§ë¤ì´ë³´ê² ì§ë§, ì§ê¸ì ì¼ë¨ ì¹ APIìì ì í ì ìë ëª ê°ì§ ìì 를 ì´í´ë³´ê² ìµëë¤.
첫 ë²ì§¸ë¡, ì¹ìì ì´ë¯¸ì§ë¥¼ ê°ì ¸ì¤ê¸° ìíì¬ fetch() ë©ìë를 ì¬ì©í ë ì
ëë¤.blob() ë©ìëë fetchê° ìëµí ìì body컨í
ì¸ ë¥¼ Blob ì¤ë¸ì í¸ë¡ ë³íìì¼ì£¼ê³ <img> ì리먼í¸ì ííí©ëë¤. ì´ìì ë first article of the seriesì ì¬í©ëë¤. ë¤ë§ Promise를 ì¬ì©í기 ìí´ ì½ê°ì ë³ê²½ì íê² ìµëë¤.
ì°¸ê³ :
The following example will not work if you just run it directly from the file (i.e. via a file:// URL). You need to run it through a local testing server, or use an online solution such as Glitch or GitHub pages.
-
먼ì simple HTML template ì fetchí ì´ë¯¸ì§ì¸ sample image file ì ë¤ì´ë°ìµëë¤.
-
HTML
<body>íë¨ì<script>ì리먼í¸ë¥¼ ì½ì í©ëë¤. -
<script>ìë¦¬ë¨¼í¸ ìì ìëì ê°ì´ ì½ë를 ìì±í©ëë¤. :jslet promise = fetch("coffee.jpg");fetch()ë©ìë를 í¸ì¶íì¬, ë¤í¸ìí¬ìì fetchí ì´ë¯¸ì§ì URLì 매ê°ë³ìë¡ ì ë¬í©ëë¤. ë ë²ì§¸ 매ê°ë³ì를 ì¬ì©í ì ìì§ë§, ì§ê¸ì ì°ì ê°ë¨íê² íëì 매ê°ë³ìë§ ì¬ì©íê² ìµëë¤. ì½ë를 ë ì´í´ë³´ë©´promiseë³ììfetch()ìì ì¼ë¡ ë°íë Promise ì¤ë¸ì í¸ë¥¼ ì ì¥íê³ ììµëë¤. ì´ì ì ë§íë¯ì´, ì§ê¸ ì¤ë¸ì í¸ë ì±ê³µë ìëê³ ì¤í¨ë ìë ì¤ê° ìí를 ì ì¥íê³ ììµëë¤. ê³µìì ì¼ë¡ë pendingìíë¼ê³ ë¶ë¦ ëë¤. -
ìì ì´ ì±ê³µì ì¼ë¡ ì§íë ë를 ëìí기 ìí´ (ì´ë² ìì ìì
Responseê° ë°íë ë ì ëë¤. ), ì°ë¦¬ë Promise ì¤ë¸ì í¸ì.then()ë©ìë를 í¸ì¶í©ëë¤..then()ë¸ë ìì callbackì (executor ë¼ê³ ë¶ë¦) Promiseê° ì±ê³µì ì¼ë¡ ìë£ëê³Responseì¤ë¸ì í¸ë¥¼ ë°íí ëë§ ì¤íí©ëë¤. â ì´ë ê² ì±ê³µí Promiseì ìí를 fulfilledë¼ê³ ë¶ë¦ ëë¤. ê·¸ë¦¬ê³ ë°íëResponseì¤ë¸ì í¸ë¥¼ 매ê°ë³ìë¡ ì ë¬í©ëë¤.ì°¸ê³ : The way that a
.then()block works is similar to when you add an event listener to an object usingAddEventListener(). It doesn't run until an event occurs (when the promise fulfills). The most notable difference is that a .then() will only run once for each time it is used, whereas an event listener could be invoked multiple times.ê·¸ë¦¬ê³ ì¦ì
blob()ë©ìë를 ì¤ííì¬ Response Bodyê° ìì í ë¤ì´ë¡ë ëëì§ íì¸í©ëë¤. ê·¸ë¦¬ê³ Response Bodyê° ì´ì© ê°ë¥í ë ì¶ê° ìì ì í ì ìëBlobì¤ë¸ì í¸ë¡ ë³íìíµëë¤. í´ë¹ ì½ëë ìëì ê°ì´ ìì±í ì ììµëë¤. :js(response) => response.blob();ìì ì½ëë ìëì ì½ë를 ì¶ì½í ííì ëë¤.
jsfunction(response) { return response.blob(); }ì´ì ì¶ê° ì¤ëª ì ì¶©ë¶íë¯ë¡, JavaScriptì 첫 ë²ì§¸ ì¤ ìëì ë¤ìê³¼ ê°ì ë¼ì¸ì ì¶ê°íì¸ì.
jslet promise2 = promise.then((response) => response.blob()); -
ê°
.then()ì í¸ì¶íë©´ ìë¡ì´ Promise를 ë§ëëë°, ì´ë ë§¤ì° ì ì©í©ëë¤. ìëíë©´blob()ë©ìëë Promise를 ë°íí기 ë문ì, ë ë²ì§¸ Promiseì.then()ë©ìë를 í¸ì¶í¨ì¼ë¡ì¨ ì´íì ë°íëëBlobì¤ë¸ì í¸ë¥¼ ì²ë¦¬í ì ììµëë¤. í ê°ì§ ë©ìë를 ì¤ííì¬ ê²°ê³¼ë¥¼ ë°ííë ê²ë³´ë¤ Blobì ì¢ ë ë³µì¡í ì¼ì ì¶ê°íê³ ì¶ìµëë¤. ì´ë´ëë ì¤ê´í¸{ }ë¡ ë¬¶ìµëë¤. (ê·¸ë ì§ ìì¼ë©´ ìë¬ê° ë°ìí©ëë¤.).ì´ì´ì ìëì ê°ì ì½ë를 ì¶ê°í©ëë¤.:
jslet promise3 = promise2.then((myBlob) => {}); -
ì´ì executor í¨ì를 ìëì ê°ì´ ì±ìë£ìµëë¤. ì¤ê´í¸ ìì ìì±íë©´ ë©ëë¤. :
jslet objectURL = URL.createObjectURL(myBlob); let image = document.createElement("img"); image.src = objectURL; document.body.appendChild(image);ì¬ê¸°ì ì°ë¦¬ë ë ë²ì§¸ Promiseê° fulfillsì¼ ë ë°íë Blobì 매ê°ë³ìë¡ ì ë¬ë°ë
URL.createObjectURL()ë©ìë를 ì¤ííê³ ììµëë¤. ì´ë ê² íë©´ ì¤ë¸ì í¸ê° ê°ì§ê³ ìë URLì´ ë°íë©ëë¤. ê·¸ ë¤ì<img>ì리먼í¸ë¥¼ ë§ë¤ê³ , ë°íë URLìsrcìì±ì ì§ì íì¬ DOMì ì¶ê°í©ëë¤. ì´ë ê² íë©´ íì´ì§ì ê·¸ë¦¼ì´ íìë©ëë¤.
ë°©ê¸ ë§ë HTML íì¼ì ì ì¥íê³ ë¸ë¼ì°ì ìì ë¶ë¬ì¤ë©´, ì´ë¯¸ì§ê° ììëë¡ íì´ì§ì íìëë ê²ì ë³¼ ì ììµëë¤. ì íì ¨ìµëë¤!
ì°¸ê³ :
You will probably notice that these examples are somewhat contrived. You could just do away with the whole fetch() and blob() chain, and just create an <img> element and set its src attribute value to the URL of the image file, coffee.jpg. We did, however, pick this example because it demonstrates promises in a nice simple fashion, rather than for its real-world appropriateness.
Responding to failure
íì¬ ìë¬ê° ë°ìíì ë ì´ë»ê² ì²ë¦¬ë¥¼ í´ì¼í ì§ ìì±ë ì½ëê° ì기ë문ì ì½ë를 ì¡°ê¸ë§ ë ì¶ê°íì¬ ì¢ ë ìë²½íê² ìì±í´ë´
ìë¤. (Promiseìì ìë¬ê° ë°ìí ìí를 rejectsë¼ ë¶ë¦
ëë¤). ì´ì ì ë´¤ëëë¡ .catch() ë¸ëì ì¶ê°íì¬ ì¤ë¥ë¥¼ í¸ë¤ë§ í ì ììµëë¤. ìëì²ë¼ ë§ì´ì£ :
let errorCase = promise3.catch((e) => {
console.log(
"There has been a problem with your fetch operation: " + e.message,
);
});
ìë¬ ë©ìì§ë¥¼ íì¸íê³ ì¶ì¼ë©´ ì못ë urlì ì§ì í´ë³´ì¸ì, ê°ë°ì ë구 ì½ììì ìë¬ë¥¼ íì¸í ì ììê² ì ëë¤.
ë¬¼ë¡ .catch() ë¸ë¡ ìì´ ì½ë를 ìëìí¬ ì ììµëë¤. íì§ë§ ì¢ ë ê¹ê² ìê°í´ë³´ë©´ .catch() ë¸ë¡ì´ ìì¼ë©´ ì´ë¤ ìë¬ê° ë°ìíëì§, ì´ë»ê² í´ê²°í´ì¼ íëì§ ëë²ê¹
ì´ ì´ë µìµëë¤. ì¤ì ì±ìì .catch() ì ì¬ì©íì¬ ì´ë¯¸ì§ ê°ì ¸ì¤ê¸°ë¥¼ ë¤ì ì¤ííê±°ë, 기본 ì´ë¯¸ì§ë¥¼ íìíë ë± ìì
ì ì§ìí ì ììµëë¤.
ì°¸ê³ : You can see our version of the example live (see the source code also).
Chaining the blocks together
ììì ì¬ì©í ì½ëë ìì
ì´ ì´ë»ê² ì²ë¦¬ëëì§ ëª
ííê² ë³´ì¬ì£¼ê¸° ìí´ ë§¤ì° ê¸¸ê² ì½ë를 ìì±íìµëë¤. ì´ì ê¸ìì ë´¤ë¯ì´, .then() ë¸ëì ì¬ì©íì¬ ì°ì ìì
ì ì§íí ì ììµëë¤. (ëí .catch() ë¸ëì ì¬ì©íì¬ ìë¬ ì²ë¦¬ë íì§ì). ìì ìì ì ì½ëë ìëì ê°ì´ ìì±í ìë ììµëë¤. (see also simple-fetch-chained.html on GitHub):
fetch("coffee.jpg")
.then((response) => response.blob())
.then((myBlob) => {
let objectURL = URL.createObjectURL(myBlob);
let image = document.createElement("img");
image.src = objectURL;
document.body.appendChild(image);
})
.catch((e) => {
console.log(
"There has been a problem with your fetch operation: " + e.message,
);
});
fulfilled promise ê²°ê³¼ì ìí´ ë°íë ê°ì´ ë¤ì .then() ë¸ë¡ì executor í¨ìê° ê°ì§ íë¼ë¯¸í°ë¡ ì ë¬ ëë¤ë ê²ì ê¼ ê¸°ìµíì¸ì.
ì°¸ê³ :
.then()/.catch() blocks in promises are basically the async equivalent of a try...catch block in sync code. Bear in mind that synchronous try...catch won't work in async code.
Promise terminology recap
ìì ì¹ì ìì ë¤ë£¬ ë´ì©ì ì ë§ ë§ìµëë¤. ë§¤ì° ì¤ìí ë´ì©ì ë¤ë¤ì¼ë¯ë¡ ê°ë ì ëª íí ì´í´í기 ìí´ ëªë²ì´ê³ ë¤ì ì½ì´ë³´ëê² ì¢ìµëë¤.
- Promiseê° ìì±ëë©´ ê·¸ ìíë ì±ê³µë ì¤í¨ë ìë pendingìíë¼ê³ ë¶ë¦ ëë¤..
- Promiseê²°ê³¼ê° ë°íëë©´ ê²°ê³¼ì ìê´ ìì´ resolvedìíë¼ê³ ë¶ë¦
ëë¤..
- ì±ê³µì ì¼ë¡ ì²ë¦¬ë Promiseë fulfilledìíì´ë¤. ì´ ìíê° ëë©´ Promise ì²´ì¸ì ë¤ì
.then()ë¸ëìì ì¬ì©í ì ìë ê°ì ë°íí©ëë¤.. 그리ê³.then()ë¸ë ë´ë¶ì executor í¨ìì Promiseìì ë°íë ê°ì´ íë¼ë¯¸í°ë¡ ì ë¬ë©ëë¤.. - ì¤í¨í Promiseë rejectedìíì´ë¤. ì´ë ì´ë¤ ì´ì (reason) ë문ì Promiseê° rejected ëëì§ë¥¼ ëíë´ë ìë¬ ë©ìì§ë¥¼ í¬í¨í ê²°ê³¼ê° ë°íë©ëë¤. Promise ì²´ì´ëì ì ì¼ ë§ì§ë§
.catch()ìì ìì¸í ìë¬ ë©ìì§ë¥¼ íì¸í ì ììµëë¤.
- ì±ê³µì ì¼ë¡ ì²ë¦¬ë Promiseë fulfilledìíì´ë¤. ì´ ìíê° ëë©´ Promise ì²´ì¸ì ë¤ì
Running code in response to multiple promises fulfilling
ìì ìì ìì Promiseì¬ì©ì 기ì´ë¥¼ íì¸íìµëë¤. ì´ì ê³ ê¸ ê¸°ë¥ë¤ì íë² ë³´ê² ìµëë¤. ì ì¼ ë¨¼ì íì¸í´ë³¼ ìì ë ë¤ìê³¼ ê°ìµëë¤. ì°ìì ì¼ë¡ ì¼ì´ëë ìì ì ì¢ìµëë¤. ê·¸ë°ë° 모ë Promiseê° fulfilledì¼ ê²½ì° ì½ë를 ì¤ííê³ ì¶ì ê²½ì°ê° ììê² ì ëë¤.
í´ë¹ 기ë¥ì Promise.all() ì´ë¼ë ì¤í
í± ë©ìë를 ì¬ì©íì¬ ë§ë¤ ì ììµëë¤. ì´ ë©ìëë Promiseì ë°°ì´ì 매ê°ë³ìë¡ ì¼ê³ , ë°°ì´ì 모ë Promiseê° fulfilì¼ ëë§ ìë¡ì´ fulfil Promise ì¤ë¸ì í¸ë¥¼ ë°íí©ëë¤. ìëì²ë¼ ë§ì´ì£ :
Promise.all([a, b, c]).then(values => {
...
});
ë°°ì´ì 모ë Promiseê° fulfil ì´ë©´, .then() ë¸ëì executor í¨ìë¡ì 매ê°ë³ìë¡ Promise ê²°ê³¼ì ë°°ì´ì ì ë¬í©ëë¤. Promise.all() ì Promiseì ë°°ì´ ì¤ íëë¼ë rejectë¼ë©´, ì ì²´ ê²°ê³¼ê° rejectê° ë©ëë¤.
ì´ ë°©ë²ì ë§¤ì° ì ì©í©ëë¤. ì¹ UIì 컨í ì¸ ë¥¼ ëì ì¸ ë°©ë²ì¼ë¡ ì±ì´ë¤ê³ ìê° í´ë³´ê² ìµëë¤. ëë¶ë¶ ê²½ì°ì ë¬ì±ë¬ì± ë´ì©ì ì±ì°ê¸°ë³´ë¨, ìì í ë´ì©ì ì±ì¸ê² ì ëë¤.
ë¤ë¥¸ ìì 를 ë§ë¤ì´ì ì¤íí´ ë³´ê² ìµëë¤.
-
ì´ë¯¸ ë§ë¤ì´ì§ page templateì ë¤ì´ë°ì¼ì¸ì 그리ê³
</body>ë¤ì<script>ì리먼í¸ë¥¼ ë§ë¤ì´ì£¼ì¸ì. -
ì´ë¯¸ì§ ê·¸ë¦¬ê³ í ì¤í¸ íì¼(coffee.jpg, tea.jpg, and description.txt)ì ë¤ì´ë°ê³ page template ì ê°ì ê²½ë¡ì ì ì¥í´ì£¼ì¸ì.
-
먼ì Promise를 ë°ííë ëª ê°ì§ í¨ì를 ë§ë¤ì´
Promise.all()ë¡ ê²°ê³¼ë¥¼ ë°íí©ëë¤. ì¸ ê°ìfetch()ìì ì´ ëëê³ ë¤ì ìì²ì ì§ííê³ ì¶ë¤ë©´ ìë ì½ëì²ë¼Promise.all()ë¸ëì ìì±í©ëë¤. :jslet a = fetch(url1); let b = fetch(url2); let c = fetch(url3); Promise.all([a, b, c]).then(values => { ... });Promiseê° fulfilledê° ëì ë, fulfilment handler í¸ë¤ë¬ë¡ ì ë¬ë values 매ê°ë³ììë ê°
fetch()ì ê²°ê³¼ë¡ ë°ìí ì¸ ê°ìResponseì¤ë¸ì í¸ê° ë¤ì´ììµëë¤ .íì§ë§ ì°ë¦¬ë ë¨ìí ê²°ê³¼ë§ ëê²¨ì£¼ê³ ì¶ì§ ììµëë¤. ì°ë¦¬ë
fetch()ì¸ì ëëëì§ ë³´ë¤ ë¶ë¬ì¨ ë°ì´í°ì ë ê´ì¬ì´ ììµëë¤. ê·¸ë§ì ë¸ë¼ì°ì ì ííí ì ìë Blobê³¼ í ì¤í¸ 문ìì´ì´ ë¶ë¬ì ì¡ì ëPromise.all()ë¸ëì ì¤ííê³ ì¶ë¤ë ê² ì ëë¤.<script>ì리먼í¸ì ìëì ê°ì´ ìì±í©ëë¤. :jsfunction fetchAndDecode(url, type) { return fetch(url) .then((response) => { if (type === "blob") { return response.blob(); } else if (type === "text") { return response.text(); } }) .catch((e) => { console.log( "There has been a problem with your fetch operation: " + e.message, ); }); }ì´ì§ ë³µì¡í´ ë³´ì´ë¯ë¡ íëíë ì´í´ë´ ìë¤. :
-
먼ì fetchAndDecode() í¨ì를 ì ìíê³ í¨ìì 매ê°ë³ìë¡ ì»¨í ì¸ ì URLê³¼ ê°ì ¸ì¤ë 리ìì¤ì íì ì ì§ì í©ëë¤.
-
í¨ì ë´ë¶ì 첫 ë²ì§¸ ììì 본 ê²ê³¼ ì ì¬í 구조를 ê°ì§ ì½ëê° ììµëë¤. â
fetch()í¨ì를 í¸ì¶íì¬ ì ë¬ë°ì URLìì 리ìì¤ë¥¼ ë°ìì¤ëë¡ íìµëë¤. ê·¸ë¦¬ê³ ë¤ì Promise를 ì°ìì ì¼ë¡ í¸ì¶íì¬ ëì½ë©ë (í¹ì "ì½ì") Response Body를 ë°ííê² í©ëë¤. ì´ì ììì Blobë§ì ê°ì ¸ì¤ê¸° ë문ìblob()ë©ìëë§ ì¼ìµëë¤. -
ì¬ê¸°ìì ì´ì ê³¼ ë¤ë¥¸ ë ê°ì§ê° ììµëë¤. :
- 먼ì ë ë²ì§¸ Promiseììë ë¶ë¬ì¬ 리ìì¤ì
typeì´ ë¬´ìì¸ì§ì ë°ë¼ ë°íë°ë ë°ì´í°ê° ë¤ë¦ ëë¤. executor í¨ì ë´ë¶ì, ê°ë¨íif ... else if구문ì ì¬ì©íì¬ ì´ë¤ ì¢ ë¥ì íì¼ì ëì½ë©í´ì¼ íëì§ì ë°ë¼ ë¤ë¥¸ Promise를 ë°ííê² íìµëë¤. (ì´ ê²½ì°blobì´ëtextë°ì ìì§ë§, ì´ê²ì ì íì©íì¬ ë¤ë¥¸ ì½ëì íì¥íì¬ ì ì©í ì ììµëë¤.). - ë ë²ì§¸ë¡,
fetch()í¸ì¶ ììreturní¤ìë를 ì¶ê°íìµëë¤. ì´ë ê² íë©´ Promise ì²´ì´ëì ë§ì§ë§ ê²°ê³¼ê°ì í¨ìì ê²°ê³¼ë¡ ë°íí´ ì¤ ì ììµëë¤. (ì´ ê²½ì°blob()í¹ìtext()ë©ìëì ìí´ ë°íë Promise ì ëë¤.) ì¬ì¤ìfetch()ììreturn구문ì ì²´ì´ë 결과를 ë¤ì ìë¨ì¼ë¡ ì ë¬íë íì ì ëë¤.
- 먼ì ë ë²ì§¸ Promiseììë ë¶ë¬ì¬ 리ìì¤ì
-
ë¸ëì ë§ì§ë§ìë
.catch()ë¸ëì ì¶ê°íì¬ ìì ì¤ ë°ìí ìë¬ë¥¼.all()ì ë°°ì´ë¡ ì ë¬í©ëë¤. ì무 Promiseìì rejectê° ë°ìíë©´, catch ë¸ëì ì´ë¤ Promiseìì ìë¬ê° ë°ìíëì§ ìë ¤ì¤ ê² ì ëë¤..all()(ìë쪽ì ìë) ë¸ëì 리ìì¤ì 문ì ê° ìì§ ìë ì´ì íì fulfilì¼ê² ì ëë¤..allë¸ëì ë§ì§ë§ ì²´ì´ëì.catch()ë¸ëì ì¶ê°íì¬ rejectëìë íì¸ì í ì ììµëë¤.
í¨ìì body ìì ìë ì½ëë ë¹ë기ì ì´ê³ Promise 기ë°ì´ë¯ë¡, ì ì²´ í¨ìë Promiseë¡ ìëí©ëë¤. â í¸ë¦¬íì£ ?.
-
-
ë¤ìì¼ë¡ fetchAndDecode() í¨ì를 ì¸ ë² í¸ì¶íì¬ ì´ë¯¸ì§ì í ì¤í¸ë¥¼ ê°ì ¸ì¤ê³ ëì½ë© íë ê³¼ì ì ììí©ëë¤. ê·¸ë¦¬ê³ ë°íë Promise를 ê°ê°ì ë³ìì ì ì¥í©ëë¤. ì´ì ì½ëì ì´ì´ì ìë ì½ë를 ì¶ê°íì¸ì. :
jslet coffee = fetchAndDecode("coffee.jpg", "blob"); let tea = fetchAndDecode("tea.jpg", "blob"); let description = fetchAndDecode("description.txt", "text"); -
ë¤ìì¼ë¡ ìì ì¸ ê°ì§ ì½ëê° ëª¨ë fulfilledê° ëì ë ìíë ì½ë를 ì¤íí기 ìí´
Promise.all()ë¸ëì ë§ëëë¤. ì°ì ,.then()call ìì ë¹ì´ìë executor 를 ì¶ê°íì¸ì :jsPromise.all([coffee, tea, description]).then((values) => {});ììì Promise를 í¬í¨íë ë°°ì´ì ë§¤ê° ë³ìë¡ ì¬ì©íë ê²ì íì¸í ì ììµëë¤. executorë ì¸ ê°ì§ Promiseê° resolveë ëë§ ì¤íë ê² ì ëë¤. ê·¸ë¦¬ê³ executorê° ì¤íë ë ê°ë³ì ì¸ Promiseì 결과를 í¬í¨íë [coffee-results, tea-results, description-results] ë°°ì´ì ë§¤ê° ë³ìë¡ ì ë¬ë°ì ê² ì ëë¤. (ì¬ê¸°ì ëì½ë©ë Response Body ì ëë¤.).
-
ë§ì§ë§ì¼ë¡ executor í¨ì를 ìì±í©ëë¤. ìì ìì ë°íë 결과를 ë³ëì ë³ìë¡ ì ì¥í기 ìí´ ê°ë¨í ë기í ì½ë를 ì¬ì©í©ëë¤. (Blobìì ì¤ë¸ì í¸ URLs ìì±), ê·¸ë¦¬ê³ íì´ì§ì í ì¤í¸ì ì´ë¯¸ì§ë¥¼ íìí©ëë¤.
jsconsole.log(values); // Store each value returned from the promises in separate variables; create object URLs from the blobs let objectURL1 = URL.createObjectURL(values[0]); let objectURL2 = URL.createObjectURL(values[1]); let descText = values[2]; // Display the images in <img> elements let image1 = document.createElement("img"); let image2 = document.createElement("img"); image1.src = objectURL1; image2.src = objectURL2; document.body.appendChild(image1); document.body.appendChild(image2); // Display the text in a paragraph let para = document.createElement("p"); para.textContent = descText; document.body.appendChild(para); -
ì½ë를 ì ì¥íê³ ì°½ì ìë¡ê³ ì¹ë©´ 보기ì ì¢ì§ ìì§ë§, UI êµ¬ì± ììê° ëª¨ë íìë ê²ì ë³¼ ì ììµëë¤.
ì¬ê¸°ì ì ê³µí ì½ëë ë§¤ì° ê¸°ì´ì ì´ì§ë§, ë´ì©ì ì ë¬í기ìë ì주 ì¢ìµëë¤..
ì°¸ê³ : If you get stuck, you can compare your version of the code to ours, to see what it is meant to look like â see it live, and see the source code.
ì°¸ê³ :
If you were improving this code, you might want to loop through a list of items to display, fetching and decoding each one, and then loop through the results inside Promise.all(), running a different function to display each one depending on what the type of code was. This would make it work for any number of items, not just three.
Also, you could determine what the type of file is being fetched without needing an explicit type property. You could, for example, check the Content-Type HTTP header of the response in each case using response.headers.get("content-type"), and then react accordingly.
Running some final code after a promise fulfills/rejects
Promiseì ê²°ê³¼ê° fulfilled ì¸ì§ rejectedì¸ì§ ê´ê³ ìì´ Promiseê° ìë£ë í ìµì¢
ì½ë ë¸ëì ì¤ííë ¤ë ê²½ì°ê° ìì ê²ì
ëë¤. ì´ì ìë ìë ììì²ë¼ .then() ë¸ëê³¼.catch() ë¸ëì callbacksì ìëì ê°ì´ runFinalCode()를 ë£ìììµëë¤. :
myPromise
.then((response) => {
doSomething(response);
runFinalCode();
})
.catch((e) => {
returnError(e);
runFinalCode();
});
ë³´ë¤ ìµê·¼ì íë ë¸ë¼ì°ì ììë .finally() ë©ìë를 ì¬ì©í ì ììµëë¤. ì´ ë©ìë를 Promise ì²´ì´ëì ëì ë°°ì¹íì¬ ì½ë ë°ë³µì ì¤ì´ê³ ì¢ ë ì°ìíê² ì¼ì ì²ë¦¬í ì ììµëë¤. ìëì ê°ì´ ë§ì§ë§ ë¸ëì ì ì©í ì ììµëë¤. :
myPromise
.then((response) => {
doSomething(response);
})
.catch((e) => {
returnError(e);
})
.finally(() => {
runFinalCode();
});
ì¤ì ììë promise-finally.html demo ì ëìììµëë¤. (see the source code also). ì´ ììë ììì ë§ë¤ì´ë³¸ Promise.all() ë°ëª¨ì ëê°ì´ ìëí©ëë¤. ë¤ë§ ì´ë²ìë fetchAndDecode() í¨ìì ë¤ì ì°ì ìì
ì¼ë¡ finally() 를 í¸ì¶í©ëë¤.:
function fetchAndDecode(url, type) {
return fetch(url)
.then((response) => {
if (type === "blob") {
return response.blob();
} else if (type === "text") {
return response.text();
}
})
.catch((e) => {
console.log(
`There has been a problem with your fetch operation for resource "${url}": ` +
e.message,
);
})
.finally(() => {
console.log(`fetch attempt for "${url}" finished.`);
});
}
ì´ ë¡ê·¸ë ê° fetchìëê° ìë£ëë©´ ì½ìì ë©ìì§ë¥¼ ì¶ë ¥íì¬ ì¬ì©ììê² ìë ¤ì¤ëë¤.
ì°¸ê³ :
then()/catch()/finally() is the async equivalent to try/catch/finally in sync code.
Building your own custom promises
ì¬ê¸°ê¹ì§ ì¤ëë¼ ìê³ íì
¨ìµëë¤. ì¬ê¸°ê¹ì§ ì¤ë©´ì ì°ë¦¬ë Promise를 ì§ì ë§ë¤ì´ë´¤ìµëë¤. ì¬ë¬ ê°ì Promise를 .then() ì ì¬ì©íì¬ ì²´ì´ë íê±°ë ì¬ì©ì ì ìí¨ì를 ì¡°í©íì¬, ë¹ë기 Promiseê¸°ë° í¨ì를 ë§ë¤ììµëë¤. ì´ì ì ë§ë fetchAndDecode() í¨ìê° ì´ë¥¼ ì ë³´ì¬ì£¼ê³ ìì£ .
ë¤ìí Promise ê¸°ë° API를 ê²°í©íì¬ ì¬ì©ì ì ì í¨ì를 ë§ëë ê²ì, Promiseì í¨ê» ìíë 기ë¥ì ë§ëë ê°ì¥ ì¼ë°ì ì¸ ë°©ë²ì´ë©°, ëë¶ë¶ 모ë APIë ì´ì ê°ì ì리를 기ë°ì¼ë¡ ë§ë¤ì´ì§ê³ ììµëë¤. ê·¸ë¦¬ê³ ë ë¤ë¥¸ ë°©ë²ì´ ììµëë¤.
Using the Promise() constructor
Promise() constructor를 ì¬ì©íì¬ ì¬ì©ì ì ì Promise를 ë§ë¤ ì ììµëë¤. ì£¼ë¡ Promise기ë°ì´ ìë 구ì ë¹ë기 APIì½ë를 Promiseê¸°ë° ì½ëë¡ ë§ë¤ê³ ì¶ì ê²½ì° ì¬ì©í©ëë¤. ì´ ë°©ë²ì 구ì íë¡ì í¸ ì½ë, ë¼ì´ë¸ë¬ë¦¬, í¹ì íë ììí¬ë¥¼ ì§ê¸ì Promise ì½ëì í¨ê» ì¬ì©í ë ì ì©í©ëë¤.
ê°ë¨í ì를 ë¤ì´ ì´í´ë³´ê² ìµëë¤. â ì¬ê¸° Promiseì í¨ê» ì¬ì©ëë setTimeout() í¸ì¶ì´ ììµëë¤. â ì´ í¨ìë 2ì´ íì "Success!"ë¼ë 문ìì´ê³¼ í¨ê» resolveë©ëë¤. (íµê³¼ë resolve() í¸ì¶ì ìí´);
let timeoutPromise = new Promise((resolve, reject) => {
setTimeout(function () {
resolve("Success!");
}, 2000);
});
resolve() ìreject() ë Promiseì fulful / rejectì¼ëì ì¼ì ìíí기 ìí´ í¸ì¶í í¨ìì
ëë¤. ì´ë²ì ê²½ì° Promiseë "Success!"문ìì í¨ê» fulfill ëìµëë¤.
ë°ë¼ì ì´ Promise를 í¸ì¶í ë, ê·¸ ëì .then() ë¸ëì ì¬ì©íë©´ "Success!" 문ìì´ì´ ì ë¬ë ê²ì
ëë¤. ìë ì½ëë ê°ë¨í alertë©ìì§ë¥¼ ì¶ë ¥íë ë°©ë² ì
ëë¤. :
timeoutPromise.then((message) => {
alert(message);
});
í¹ì ìëì²ë¼ ì¸ ì ìì£
timeoutPromise.then(alert);
Try running this live to see the result (also see the source code).
ìì ììë ì ì°íê² ì ì©ë ììê° ìëëë¤. â Promiseë íì° íëì 문ìì´ë¡ë§ fulfilë©ëë¤. ê·¸ë¦¬ê³ reject() ì¡°ê±´ë ì ìëì´ìì§ ììµëë¤. (ì¬ì¤, setTimeout() ì ì¤í¨ ì¡°ê±´ì´ íìììµëë¤, ê·¸ë¬ë ì´ ìì ììë ìì´ë ë©ëë¤.).
ì°¸ê³ :
Why resolve(), and not fulfill()? The answer we'll give you, for now, is it's complicated.
Rejecting a custom promise
reject() ë©ìë를 ì¬ì©íì¬ Promiseê° rejectìíì¼ ë ì ë¬í ê°ì ì§ì í ì ììµëë¤. â resolve()ì ëê°ìµëë¤. ì¬ê¸°ì íëì ê°ë§ ë¤ì´ê° ì ììµëë¤. Promiseê° reject ëë©´ ìë¬ë .catch() ë¸ëì¼ë¡ ì ë¬ë©ëë¤.
ì´ì ìì를 ì¢ ë íì¥íì¬ reject() ì ì¶ê°íê³ , Promiseê° fulfilì¼ ë ë¤ë¥¸ ë©ìì§ë ì ë¬í ì ìê² ë§ë¤ì´ë´
ìë¤.
ì´ì ìì previous example를 ë³µì¬í í ì´ë¯¸ ìë timeoutPromise() í¨ì를 ìëì²ë¼ ì ìí´ì£¼ì¸ì. :
function timeoutPromise(message, interval) {
return new Promise((resolve, reject) => {
if (message === "" || typeof message !== "string") {
reject("Message is empty or not a string");
} else if (interval < 0 || typeof interval !== "number") {
reject("Interval is negative or not a number");
} else {
setTimeout(function () {
resolve(message);
}, interval);
}
});
}
í¨ì를 ì´í´ë³´ë©´ ë ê°ì§ 매ê°ë³ìê° ììµëë¤. â ì¶ë ¥í ë©ìì§ì(message) ë©ìì§ë¥¼ ì¶ë ¥í ë ê¹ì§ 기ë¤ë¦´ ìê°(interval)ì
ëë¤. 맨 ìì Promise ì¤ë¸ì í¸ë¥¼ ë°ííëë¡ ëì´ììµëë¤. ë°ë¼ì í¨ì를 ì¤ííë©´ ì°ë¦¬ê° ì¬ì©íê³ ì¶ì Promiseê° ë°íë ê² ì
ëë¤..
Promise constructor ììë ëªê°ì§ ì¬íì íì¸í기 ìí´ if ... else êµ¬ë¬¸ì´ ììµëë¤. :
- 첫ë²ì§¸ë¡ ë©ìì§ì ì í¨ì±ì ê²ì¬í©ëë¤. ë©ìì§ê° ë¹ì´ìê±°ë 문ìê° ìë ê²½ì°, ìë¬ ë©ìì§ì í¨ê» Promise를 rejectí©ëë¤.
- ê·¸ ë¤ìì¼ë¡ intervalì ì í¨ì±ì ê²ì¬í©ëë¤. ì«ìê° ìëê±°ë ììì¼ ê²½ì°, ìë¬ ë©ìì§ì í¨ê» Promise를 rejectí©ëë¤.
- ë§ì§ë§ì í목ì, ë 매ê°ë³ì를 íì¸íì¬ ì í¨í ê²½ì°
setTimeout()í¨ìì ì§ì ë intervalì ë§ì¶° Promise를 resolveí©ëë¤.
timeoutPromise() í¨ìë Promise를 ë°ííë¯ë¡, .then(), .catch(), 기íë±ë± ì ì¬ì©í´ Promise ì²´ì´ëì ë§ë¤ ì ììµëë¤. ìëì ê°ì´ ìì±í´ë´
ìë¤. â 기존ì ìë timeoutPromise 를 ìì íê³ ìëì²ë¼ ë°ê¿ì£¼ì¸ì. :
timeoutPromise("Hello there!", 1000)
.then((message) => {
alert(message);
})
.catch((e) => {
console.log("Error: " + e);
});
ì´ ì½ë를 ì ì¥íê³ ë¸ë¼ì°ì 를 ìë¡ ê³ ì¹¨íë©´ 1ì´ íì 'Hello there!' alertê° ì¶ë ¥ë ê² ì ëë¤. ì´ì ë©ìì§ ë´ì©ì ë¹ì°ê±°ë intervalì ììë¡ ì§ì í´ë³´ì¸ì ê·¸ë ê² íë©´ Promiseê° rejectëë©° ìë¬ ë©ìì§ë¥¼ ì½ìì ì¶ë ¥í´ ì¤ ê²ì ëë¤. ëí resolved ë©ìì§ë¥¼ ë¤ë¥´ê² ë§ë¤ì´ ì¤ ìë ììµëë¤.
ì°¸ê³ : You can find our version of this example on GitHub as custom-promise2.html (see also the source code).
A more real-world example
ìì ìì ë ê°ë
ì ì´í´í기 ì½ê² ë¨ìíê² ë§ë¤ìì§ë§, ì¤ì ë¡ ê·¸ë¤ì§ ë¹ë기ì ì´ì§ë ììµëë¤. ìµì§ë¡ ë¹ë기ì ìì
ì 구íí기 ìí´ setTimeout()ì ì¬ì©íì¬ í¨ì를 ë§ë¤ìì§ë§ ì¬ì©ì ì ì Promise를 ë§ë¤ê³ ìë¬ë¥¼ ë¤ë£¨ê¸°ì ì¶©ë¶í ìì ììµëë¤.
ì¢ ë ê³µë¶í´ë³¼ ì¶ê°ë´ì©ì ìê°í´ì£¼ê³ ì¶ìµëë¤. ë°ë¡ Jake Archibald's idb libraryì
ëë¤ ì´ ë¼ì´ë¸ë¬ë¦¬ë Promise() constructorì ë¹ë기ìì
ìì©ì ë³´ì¬ì£¼ë ì ì©í ë¼ì´ë¸ë¬ë¦¬ ì
ëë¤. í´ë¼ì´ì¸í¸ì¸¡ìì ë°ì´í°ë¥¼ ì ì¥íê³ ê²ìí기 ìí 구ì callback ê¸°ë° APIë¡ Promiseì í¨ê» ì¬ì©íë IndexedDB APIì
ëë¤. main library fileì ì´í´ë³´ë©´ ì°ë¦¬ê° ì§ê¸ê¹ì§ ë¤ë¤ë³¸ê²ê³¼ ê°ì ì¢
ë¥ì í
í¬ëì ë³¼ ì ììµëë¤. ìë ì½ë ë¸ë¡ì basic request modelì´ Promise를 ì¬ì©íê²ë ë³íí´ ì£¼ë IndexedDB ë©ìë ì
ëë¤. :
function promisifyRequest(request) {
return new Promise(function (resolve, reject) {
request.onsuccess = function () {
resolve(request.result);
};
request.onerror = function () {
reject(request.error);
};
});
}
ì°ë¦¬ê° íëê² ì²ë¼ ì ì í íì´ë°ì Promise를 fulfilíê³ rejectíë ì´ë²¤í¸ í¸ë¤ë¬ë¥¼ ë ê° ì¶ê°íìµëë¤. :
requestìsuccesseventê° ì¤íë ë,onsuccessí¸ë¤ë¬ì ìí´ fulfillë Promiseì requestresult를 ë°ííë¤.- ë°ë©´
request'serroreventê° ì¤íëë©´onerrorí¸ë¤ë¬ì ìí´ rejectë Promiseì requesterror를 ë°ííë¤.
Conclusion
Promiseë í¨ìì ë°í ê°ì ì측í ì ìê±°ë ìë£ë기ê¹ì§ ìê°ì´ 걸리ë ë¹ë기 ì í리ì¼ì´ì
ì 구ì¶í ë ì ì©í ë°©ë²ì
ëë¤. Promise를 ì¬ì©íë©´ ê¹ê² ì¤ì²©ë ì½ë°±(callback) ìì´ë ì°ìì ì¸ ë¹ë기 ìì
ì ë ì½ê² íííê³ ì´í´í ì ìì¼ë©°, ë기 ì½ëì try...catch 문과 ì ì¬í ë°©ìì ì¤ë¥ ì²ë¦¬ë¥¼ ì§ìí©ëë¤.
Promiseë 모ë ìµì ë¸ë¼ì°ì ì ìµì ë²ì ìì ìëí©ëë¤. Promise ì§ìì 문ì ê° ëë ê³³ì Opera Miniì IE11 ë° ê·¸ ì´ì ë²ì ë¿ì ëë¤.
ì´ ê¸ììë Promiseì 모ë 기ë¥ì ë¤ë£¨ì§ë ììê³ , ê°ì¥ í¥ë¯¸ë¡ê³ ì ì©í ê²ë¤ë§ ì´í´ë³´ììµëë¤. Promiseì ëí´ ë ê¹ì´ ë°°ì°ë¤ ë³´ë©´, ë ë§ì 기ë¥ê³¼ 기ë²ë¤ì ì íê² ë ê²ì ëë¤.
ëë¶ë¶ì ìµì ì¹ APIë Promise 기ë°ì´ë¯ë¡, ì´ë¥¼ ìµëí íì©íë ¤ë©´ Promise를 ì´í´í´ì¼ í©ëë¤. ì´ë¬í APIìë WebRTC, Web Audio API, Media Capture and Streams, ë±ì´ ìì¼ë©°, ì´ ë°ìë ë§ì´ ììµëë¤. Promiseë ìì¼ë¡ ì ì ë ì¤ìí´ì§ ê²ì´ë¯ë¡, Promiseì ì¬ì©ë²ê³¼ ëì ì리를 ë°°ì°ë ê²ì ìµì JavaScript íìµì ì¤ìí ê³¼ì ì ëë¤.
See also
Promise()- Using promises
- We have a problem with promises by Nolan Lawson