/
๐Ÿ“™

JS Promise

JavaScript
Table of contents

JavaScript Promise

ํ”„๋กœ๋ฏธ์Šค๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด์ด๊ณ  ์ฃผ๋กœ ์„œ๋ฒ„์—์„œ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

ES6์—์„œ๋Š” ์ „ํ†ต์ ์ธ ์ฝœ๋ฐฑ ํŒจํ„ด์ด ๊ฐ€์ง„ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ณ  ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์‹œ์ ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๋Š” ํŒจํ„ด์œผ๋กœ Promise๋ฅผ ๋„์ž…ํ–ˆ๋‹ค.

Promise์˜ States

new Promise()๋กœ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ข…๋ฃŒ ๋  ๋•Œ๊นŒ์ง€ ์ƒํƒœ๋ฅผ ๊ฐ–๋Š”๋‹ค. (ํ”„๋กœ๋ฏธ์Šค์˜ ์ฒ˜๋ฆฌ ๊ณผ์ •)

์ƒํƒœ์˜๋ฏธ๊ตฌํ˜„
Pending(๋Œ€๊ธฐ) ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์•„์ง ์ˆ˜ํ–‰๋˜์ง€ ์•Š์€ ์ƒํƒœresolve ๋˜๋Š” reject ํ•จ์ˆ˜๊ฐ€ ์•„์ง ํ˜ธ์ถœ๋˜์ง€ ์•Š์€ ์ƒํƒœ
Fulfilled(์„ฑ๊ณต) ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋˜์–ด ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ค€ ์ƒํƒœresolve ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ ์ƒํƒœ
Rejected(์‹คํŒจ) ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์‹คํŒจํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ์ƒํƒœreject ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ ์ƒํƒœ
Settledfulfilled ๋˜๋Š” rejected์™€ ์ƒ๊ด€ ์—†์ด pending ์ด ์•„๋‹Œ ์ƒํƒœ, ์ฆ‰ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋œ ์ƒํƒœresolve ๋˜๋Š” reject ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ ์ƒํƒœ

Pending(๋Œ€๊ธฐ)

new Promise() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด pending ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.

javascript
new Promise();

new Promise() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๊ณ , ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ธ์ž๋Š” resolve, reject ํ•จ์ˆ˜์ด๋‹ค.

js
new Promise(function(resolve, reject) {
// ...
});
  • Promise ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ์ธ์ž: ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ฝœ๋ฐฑ ํ•จ์ˆ˜
  • ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•  ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ธ์ˆ˜: resolve ํ•จ์ˆ˜, reject ํ•จ์ˆ˜

Fulfilled(์ดํ–‰)

์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ธ์ž resolve๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์‹คํ–‰ํ•˜๋ฉด fulfilled ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.

javascript
new Promise(function(resolve, reject) {
resolve();
});

์ดํ–‰ ์ƒํƒœ๊ฐ€ ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด then()์„ ์ด์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

js
function getData() {
return new Promise(function(resolve, reject) {
var data = 100;
resolve(data);
});
}
// resolve()์˜ ๊ฒฐ๊ณผ ๊ฐ’ data๋ฅผ resolvedData๋กœ ๋ฐ›์Œ
getData().then(function(resolvedData) {
console.log(resolvedData); // 100
});

Rejected(์‹คํŒจ)

์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ธ์ž reject๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ํ˜ธ์ถœํ•˜๋ฉด rejected ์ƒํƒœ๊ฐ€ ๋œ๋‹ค.

js
new Promise(function(resolve, reject) {
reject();
});

์‹คํŒจ ์ƒํƒœ๊ฐ€ ๋˜๋ฉด ์‹คํŒจ ์ฒ˜๋ฆฌ์˜ ๊ฒฐ๊ณผ ๊ฐ’(์—๋Ÿฌ)์„ catch()๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

js
function getData() {
return new Promise(function(resolve, reject) {
reject(new Error("Request is failed"));
});
}
// reject()์˜ ๊ฒฐ๊ณผ ๊ฐ’ Error๋ฅผ err์— ๋ฐ›์Œ
getData().then().catch(function(err) {
console.log(err); // Error: Request is failed
});

Promise ์ฒ˜๋ฆฌ ๊ณผ์ •

promise

์ถœ์ฒ˜: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Promise ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ธฐ โ€ข Captain Pangyo

  1. Promise ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šคํ™”

    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ๋ฐ›๋Š”๋ฐ ์ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” resolve์™€ reject ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ
  2. Promise ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ˆ˜ํ–‰

  3. ํ”„๋กœ๋ฏธ์Šค๋กœ ๊ตฌํ˜„๋œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ Promise ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ

  4. ๋น„๋™๊ธฐ์‹ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ resolve์™€ reject ํ•จ์ˆ˜์— ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ฉด์„œ ํ˜ธ์ถœ

    • ์„ฑ๊ณต: resolve ํ•จ์ˆ˜์— ๊ฒฐ๊ณผ ๊ฐ’ ์ „๋‹ฌ, ํ˜ธ์ถœ
    • ์‹คํŒจ: reject ํ•จ์ˆ˜์— ๊ฒฐ๊ณผ ๊ฐ’ ์ „๋‹ฌ, ํ˜ธ์ถœ
  5. ํ”„๋กœ๋ฏธ์Šค๋กœ ๊ตฌํ˜„๋œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋Š” Promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜

    • ๋ฐ˜ํ™˜๋œ Promise ๊ฐ์ฒด๋Š” ์ƒํƒœ(state) ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜์— ๋”ฐ๋ผ์„œ ๊ฒฐ์ •๋œ๋‹ค.

    • resolve ํ•จ์ˆ˜ ํ˜ธ์ถœ: fulfilled (์„ฑ๊ณต)

    • reject ํ•จ์ˆ˜ ํ˜ธ์ถœ: rejected (์‹คํŒจ)

  6. ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๊ฐ’์€ Promise ๊ฐ์ฒด์˜ ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌ

Promise ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ

ํ”„๋กœ๋ฏธ์Šค๋กœ ๊ตฌํ˜„๋œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋Š” Promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

ํ”„๋กœ๋ฏธ์Šค๋กœ ๊ตฌํ˜„๋œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ธก(promise consumer)์—์„œ๋Š” Promise ๊ฐ์ฒด์˜ ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ then, catch, finally๋ฅผ ํ†ตํ•ด ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋˜๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ํ›„์† ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

Promise ๊ฐ์ฒด๋Š” ์ƒํƒœ๋ฅผ ๊ฐ–๋Š”๋‹ค๊ณ  ํ•˜์˜€๋‹ค.

์ด ์ƒํƒœ์— ๋”ฐ๋ผ ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฒด์ด๋‹ ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœํ•œ๋‹ค. ํ”„๋กœ๋ฏธ์Šค์˜ ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

Promise.prototype.then()

.then()์€ ๋‘ ๊ฐœ์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ ๋ฐ›๊ณ  Promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • ์ฒซ ๋ฒˆ์งธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ”„๋กœ๋ฏธ์Šค์˜ ์„ฑ๊ณต(fulfilled, resolve ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ ์ƒํƒœ) ์‹œ ํ˜ธ์ถœ
  • ๋‘ ๋ฒˆ์งธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ํ”„๋กœ๋ฏธ์Šค์˜ ์‹คํŒจ(rejected, reject ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ ์ƒํƒœ) ์‹œ ํ˜ธ์ถœ
javascript
promise.then(
function(result) {
// ...
},
function(error) {
// ...
}
);
// ES6 ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋ฌธ๋ฒ•
promise.then(
result => { /* ... */},
error => { /* ... */});

.then(null, f)

์—๋Ÿฌ๋งŒ ๋‹ค๋ฃจ๊ณ  ์‹ถ๋‹ค๋ฉด ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ null์„ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.

Promise.prototype.catch()

.catch()๋Š” ํ•œ ๊ฐœ์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌ ๋ฐ›๊ณ  Promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฃผ๋กœ ์—๋Ÿฌ๋ฅผ ํ•ธ๋“ค๋ง ํ• ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

  • ํ”„๋กœ๋ฏธ์Šค์˜ ์˜ˆ์™ธ(๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์—๋Ÿฌ, then์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ) ๋ฐœ์ƒ์‹œ ํ˜ธ์ถœ
javascript
promise.catch(
function(error) {
// ...
}
);
// ES6 ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋ฌธ๋ฒ•
promise.catch(error => { /* ... */ });

.catch(f) == .then(null,f)

์œ„์˜ ๋‘ ๊ฐœ๊ฐ€ ๋™์ผํ•˜๋‹ค๊ณ  ํ•˜๋Š” ๊ฒƒ์„ ๋ณด๋‹ˆ syntax sugar์ธ ๋“ฏ ํ•˜๋‹ค.

Promise.prototype.finally()

.finally() ์ธ์ˆ˜๊ฐ€ ์—†๊ณ  ํ”„๋กœ๋ฏธ์Šค์˜ ์ƒํƒœ์™€ ์ƒ๊ด€์—†์ด ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
.then()๊ณผ .catch() ๋ฉ”์†Œ๋“œ์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์•ผํ•  ๊ฒƒ์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ์ค‘๋ณต์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค.

.finally() ๋ฉ”์†Œ๋“œ๋Š” ํ”„๋ผ๋ฏธ์Šค ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— finally๋ฅผ ํ†ต๊ณผํ•˜์—ฌ ํ›„์† ๋ฉ”์†Œ๋“œ์— ์ „๋‹ฌ ๋œ๋‹ค.

  • ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์ฒ˜๋ฆฌ๋˜๋ฉด ์„ฑ๊ณต(fulfilled) ๋˜๋Š” ์‹คํŒจ(rejected)์™€ ์ƒ๊ด€์—†์ด ํ˜ธ์ถœ
javascript
promise.finally(
function() {
// ...
}
);
// ES6 ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋ฌธ๋ฒ•
promise.finally(() => { /* ... */ });

TC39 Stage 4
Expected Publication Year: 2018

์ฐธ์กฐ: Promise.prototype.finally() - JavaScript | MDN / tc39/proposal-promise-finally

Promise Chaining

ํ”„๋กœ๋ฏธ์Šค๋Š” ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฒด์ด๋‹(chaining)ํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋Š” ํ”„๋กœ๋ฏธ์Šค ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ์ธ then, catch, finally ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

javascript
function promiseGet() {
return new Promise({
// ...
});
}
// ํ”„๋กœ๋ฏธ์Šค ์ฒด์ด๋‹
promiseGet()
.then(function(data) {
// ...
})
.finally(function() {
// ...
})
.catch(function(error) {
// ...
});
// ES6 ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋ฌธ๋ฒ•
promiseGet()
.then(data => { /* ... */ })
.finally(() => { /* ... */ })
.catch(error => { /* ... */ });

Promise ์ •์  ๋ฉ”์†Œ๋“œ

Promise๋Š” ์ฃผ๋กœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉ๋˜์ง€๋งŒ 5๊ฐ€์ง€ ์ •์  ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. (ํ•จ์ˆ˜๋„ ๊ฐ์ฒด์ด๋ฏ€๋กœ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค)

Promise์˜ 5๊ฐ€์ง€ ์ •์  ๋ฉ”์†Œ๋“œ

  • Promise.resolve()
  • Promise.reject()
  • Promise.all()
  • Promise.race()
  • Promise.allSettled()

Promise.resolve()

Promise.resolve() ๋ฉ”์†Œ๋“œ๋Š” ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๊ฐ’์„ resolveํ•˜๋Š” ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
(์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ’์„ ํ”„๋กœ๋ฏธ์Šค๋กœ ๋ž˜ํ•‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ)

javascript
// ๋ฐฐ์—ด์„ resolveํ•˜๋Š” Promise ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
const resolvedPromise = Promise.resolve([1, 2, 3]);
resolvedPromise.then(console.log); // [1, 2, 3]

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์œ„์™€ ๋™์ผํ•˜๋‹ค.

javascript
const resolvedPromise = new Promise(resolve => resolve([1, 2, 3]));
resolvedPromise.then(console.log); // [1, 2, 3]

Promise.reject()

Promise.reject() ๋ฉ”์†Œ๋“œ๋Š” ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๊ฐ’์„ rejectํ•˜๋Š” ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
(์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ’์„ ํ”„๋กœ๋ฏธ์Šค๋กœ ๋ž˜ํ•‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ)

javascript
// ์—๋Ÿฌ ๊ฐ์ฒด๋ฅผ rejectํ•˜๋Š” Promise ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
const rejectedPromise = Promise.reject(new Error('Error!'));
rejectedPromise.catch(console.log); // Error: Error!

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์œ„์™€ ๋™์ผํ•˜๋‹ค.

javascript
const rejectedPromise = new Promise((resolve, reject) => reject(new Error('Error!')));
rejectedPromise.catch(console.log); // Error: Error!

Promise.all()

Promise.all() ๋ฉ”์†Œ๋“œ๋Š” ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋Š” ๋ฐฐ์—ด ๋“ฑ์˜ ์ดํ„ฐ๋Ÿฌ๋ธ”(์ˆœํ™˜/๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ)์„ ์ธ์ž๋กœ ์ „๋‹ฌ ๋ฐ›๊ณ  ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ชจ๋‘ ์—ฐ์†์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ทธ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ resolveํ•˜๋Š” ์ƒˆ๋กœ์šด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

javascript
Promise.all([
new Promise(resolve => setTimeout(() => resolve(1), 3000)), // 1
new Promise(resolve => setTimeout(() => resolve(2), 2000)), // 2
new Promise(resolve => setTimeout(() => resolve(3), 1000)) // 3
]).then(console.log) // [ 1, 2, 3 ]
.catch(console.error);

Promise.all์€ ๋ฐฐ์—ด ๋‚ด ๋ชจ๋“  ํ”„๋กœ๋ฏธ์Šค์˜ resolve ๋˜๋Š” ์ฒซ๋ฒˆ์งธ reject๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ณ , ๋ชจ๋“  ํ”„๋กœ๋ฏธ์Šค์˜ ์ฒ˜๋ฆฌ๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด ๊ฐ๊ฐ์˜ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ resolveํ•œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ resolveํ•˜๋Š” ์ƒˆ๋กœ์šด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ฒ˜๋ฆฌ ์ˆœ์„œ ๋ณด์žฅ

์ฒซ๋ฒˆ์งธ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๊ฐ€์žฅ ๋‚˜์ค‘์— ์ฒ˜๋ฆฌ๋˜์–ด๋„ Promise.all ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ”„๋กœ๋ฏธ์Šค๋Š” ์ฒซ๋ฒˆ์งธ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ resolveํ•œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ ๊ทธ ๋ฐฐ์—ด์„ resolveํ•˜๋Š” ์ƒˆ๋กœ์šด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

ํ”„๋กœ๋ฏธ์Šค์˜ ์ฒ˜๋ฆฌ๊ฐ€ ์‹คํŒจํ•˜๋Š” ๊ฒฝ์šฐ

์ฒ˜๋ฆฌ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์‹คํŒจํ•˜๋ฉด ๊ฐ€์žฅ ๋จผ์ € ์‹คํŒจํ•œ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ rejectํ•œ ์—๋Ÿฌ๋ฅผ rejectํ•˜๋Š” ์ƒˆ๋กœ์šด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ฆ‰์‹œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

javascript
// ์„ธ๋ฒˆ์งธ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ์‹คํŒจํ•˜๋ฏ€๋กœ ์„ธ๋ฒˆ์งธ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ rejectํ•œ ์—๋Ÿฌ๊ฐ€ catch ๋ฉ”์†Œ๋“œ๋กœ ์ „๋‹ฌ๋œ๋‹ค.
Promise.all([
new Promise((resolve, reject) => setTimeout(() => reject(new Error('Error 1!')), 3000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error('Error 2!')), 2000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error('Error 3!')), 1000))
]).then(console.log)
.catch(console.log); // Error: Error 3!

์ดํ„ฐ๋Ÿฌ๋ธ”์˜ ์š”์†Œ๊ฐ€ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ

์ „๋‹ฌ ๋ฐ›์€ ์ดํ„ฐ๋Ÿฌ๋ธ”์˜ ์š”์†Œ๊ฐ€ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ, Promise.resolve ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํ”„๋กœ๋ฏธ์Šค๋กœ ๋ž˜ํ•‘๋œ๋‹ค.

javascript
Promise.all([
1, // => Promise.resolve(1)
2, // => Promise.resolve(2)
3 // => Promise.resolve(3)
]).then(console.log) // [1, 2, 3]
.catch(console.log);

Promise.race()

Promise.race() ๋ฉ”์†Œ๋“œ๋Š” ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋Š” ๋ฐฐ์—ด ๋“ฑ์˜ ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ์ธ์ž๋กœ ์ „๋‹ฌ ๋ฐ›๊ณ  ๊ฐ€์žฅ ๋จผ์ € ์ฒ˜๋ฆฌ๋œ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ resolveํ•œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ resolveํ•˜๋Š” ์ƒˆ๋กœ์šด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

javascript
Promise.race([
new Promise(resolve => setTimeout(() => resolve(1), 3000)), // 1
new Promise(resolve => setTimeout(() => resolve(2), 2000)), // 2
new Promise(resolve => setTimeout(() => resolve(3), 1000)) // 3
]).then(console.log) // 3
.catch(console.log);

ํ”„๋กœ๋ฏธ์Šค์˜ ์ฒ˜๋ฆฌ๊ฐ€ ์‹คํŒจํ•˜๋Š” ๊ฒฝ์šฐ

์ฒ˜๋ฆฌ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์‹คํŒจํ•˜๋ฉด ๊ฐ€์žฅ ๋จผ์ € ์‹คํŒจํ•œ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ rejectํ•œ ์—๋Ÿฌ๋ฅผ rejectํ•˜๋Š” ์ƒˆ๋กœ์šด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ฆ‰์‹œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
(Promise.all ๋ฉ”์†Œ๋“œ์™€ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌ)

javascript
// ์„ธ๋ฒˆ์งธ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ์‹คํŒจํ•˜๋ฏ€๋กœ ์„ธ๋ฒˆ์งธ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ rejectํ•œ ์—๋Ÿฌ๊ฐ€ catch ๋ฉ”์†Œ๋“œ๋กœ ์ „๋‹ฌ๋œ๋‹ค.
Promise.race([
new Promise((resolve, reject) => setTimeout(() => reject(new Error('Error 1!')), 3000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error('Error 2!')), 2000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error('Error 3!')), 1000))
]).then(console.log)
.catch(console.log); // Error: Error 3!

Promise.all vs Promise.race

Promise.all ๋ฉ”์†Œ๋“œ์ฒ˜๋Ÿผ ๋ชจ๋“  ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์—ฐ์†์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐ€์žฅ ๋จผ์ € ์ฒ˜๋ฆฌ๋œ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ resolveํ•œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ resolveํ•˜๋Š” ์ƒˆ๋กœ์šด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • Promise.all() : ๋ชจ๋“  ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์—ฐ์†์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ˆœ์„œ๋Œ€๋กœ ๋ชจ๋‘ ๋ฐ˜ํ™˜
  • Promise.race() : ๊ฐ€์žฅ ๋จผ์ € ์ฒ˜๋ฆฌ๋œ ํ”„๋กœ๋ฏธ์Šค๋งŒ ๋ฐ˜ํ™˜

Promise.allSettled()

Promise.allSettled() ๋Š” ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋Š” ๋ฐฐ์—ด ๋“ฑ์˜ ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ์ธ์ž๋กœ ์ „๋‹ฌ ๋ฐ›๊ณ  ๋ชจ๋“  ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ชจ๋‘ ์—ฐ์†์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ์ƒํƒœ์™€ ๊ด€๊ณ„์—†์ด ๊ทธ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

javascript
Promise.allSettled([
new Promise(resolve => setTimeout(() => resolve(1), 2000)),
new Promise((resolve, reject) => setTimeout(() => reject(new Error('Error!')), 1000))
]).then(console.log);

ํ”„๋กœ๋ฏธ์Šค์˜ ์ฒ˜๋ฆฌ ์ƒํƒœ์™€ ์ƒ๊ด€์—†์ด ๋ชจ๋‘ ๋ฐ˜ํ™˜

fulfilled ๋˜๋Š” rejected ์ƒ๊ด€์—†์ด ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ชจ๋“  ํ”„๋กœ๋ฏธ์Šค๋“ค์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

javascript
// Promise.allSettled ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฐ์—ด
[
// ํ”„๋กœ๋ฏธ์Šค๊ฐ€ fulfilled ์ƒํƒœ์ธ ๊ฒฝ์šฐ, ํ”„๋กœ๋ฏธ์Šค์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ
{status: "fulfilled", value: 1},
// ํ”„๋กœ๋ฏธ์Šค๊ฐ€ rejected ์ƒํƒœ์ธ ๊ฒฝ์šฐ, ํ”„๋กœ๋ฏธ์Šค์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ
{status: "rejected", reason: Error: Error! at ...}
]
  • fulfilled ์ƒํƒœ์ธ ๊ฒฝ์šฐ: ์ฒ˜๋ฆฌ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” status ํ”„๋กœํผํ‹ฐ, ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” value ํ”„๋กœํผํ‹ฐ
    • {status: "fulfilled", value: /* ๊ฒฐ๊ณผ ๊ฐ’ */ }
  • rejected ์ƒํƒœ์ธ ๊ฒฝ์šฐ: ์ฒ˜๋ฆฌ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” status ํ”„๋กœํผํ‹ฐ, ์—๋Ÿฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” reason ํ”„๋กœํผํ‹ฐ
    • {status: "rejected", reason: /* ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ */ }

TC39 Stage 4 Draft
Expected Publication Year: 2020

Promise.allSettled() - JavaScript | MDN / tc39/proposal-promise-allSettled

Callback ํŒจํ„ด๊ณผ Promise

๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ „ํ†ต์ ์ธ ์ฝœ๋ฐฑ ํŒจํ„ด์€ ๊ฐ€๋…์„ฑ์ด ๋‚˜์˜๊ณ  ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ค‘ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ์˜ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ๊ณค๋ž€ ํ•˜๋ฉฐ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์— ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค.

Callback Hell

๋น„๋™๊ธฐ ํ•จ์ˆ˜์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ง€๊ณ  ๋‹ค๋ฅธ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ด ์ค‘์ฒฉ(nesting)์ด ๋˜์–ด ๋ณต์žก๋„๊ฐ€ ๋†’์•„์ง€๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜๋Š”๋ฐ ์ด๋ฅผ ์ฝœ๋ฐฑ ํ—ฌ(Callback Hell)์ด๋ผ ํ•œ๋‹ค.

javascript
step1(function(value1) {
step2(value1, function(value2) {
step3(value2, function(value3) {
step4(value3, function(value4) {
step5(value4, function(value5) {
// value5๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ฒ˜๋ฆฌ
});
});
});
});
});
javascript
get('/step1', a => {
get(`/step2/${a}`, b => {
get(`/step3/${b}`, c => {
get(`/step4/${c}`, d => {
console.log(d);
});
});
});
});

Callback ํŒจํ„ด๊ณผ Promise ๋น„๊ต

callback ํŒจํ„ด์œผ๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ

javascript
// GET ์š”์ฒญ์„ ์œ„ํ•œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜
const get = (url, callback) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.send();
xhr.onload = () => {
if (xhr.status === 200) {
// ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ์˜ ์‘๋‹ต์„ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๋ฉด์„œ ํ˜ธ์ถœํ•˜์—ฌ ์‘๋‹ต์— ๋Œ€ํ•œ ํ›„์† ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
callback(JSON.parse(xhr.response));
} else {
console.error(`${xhr.status} ${xhr.statusText}`);
}
};
};
// id๊ฐ€ 1์ธ post์˜ userId๋ฅผ ์ทจ๋“
get('https://jsonplaceholder.typicode.com/posts/1', ({ userId }) => {
// ์ทจ๋“ํ•œ post์˜ userId๋กœ user ์ •๋ณด๋ฅผ ์ทจ๋“
get(`https://jsonplaceholder.typicode.com/users/${userId}`, userInfo => {
console.log(userInfo);
});
});

์œ„์˜ ์˜ˆ์ œ๋ฅผ promise๋กœ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ

javascript
// GET ์š”์ฒญ์„ ์œ„ํ•œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜
const promiseGet = url => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.send();
xhr.onload = () => {
if (xhr.status === 200) {
// ์„ฑ๊ณต์ ์œผ๋กœ ์‘๋‹ต์„ ์ „๋‹ฌ๋ฐ›์œผ๋ฉด resolve ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
resolve(JSON.parse(xhr.response));
} else {
// ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด reject ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
reject(new Error(xhr.status));
}
};
});
};
// id๊ฐ€ 1์ธ post์˜ userId๋ฅผ ์ทจ๋“
promiseGet('https://jsonplaceholder.typicode.com/posts/1')
// ์ทจ๋“ํ•œ post์˜ userId๋กœ user ์ •๋ณด๋ฅผ ์ทจ๋“
.then(({ userId }) => promiseGet(`https://jsonplaceholder.typicode.com/users/${userId}`))
.then(userInfo => console.log(userInfo))
.catch(err => console.error(err));

fetch

fetch() ํ•จ์ˆ˜๋Š” XMLHttpRequest ๊ฐ์ฒด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ HTTP ์š”์ฒญ ์ „์†ก ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” Web API, HTTP ์‘๋‹ต์„ ๋‚˜ํƒ€๋‚ด๋Š” Response ๊ฐ์ฒด๋ฅผ ๋ž˜ํ•‘ํ•œ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ํ›„์† ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ then์„ ํ†ตํ•ด ํ”„๋กœ๋ฏธ์Šค๊ฐ€ resolveํ•œ Response ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

fetch()์— HTTP ์š”์ฒญ์„ ์ „์†กํ•  URL๊ณผ HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ, HTTP ์š”์ฒญ ํ—ค๋”, ํŽ˜์ด๋กœ๋“œ ๋“ฑ์„ ์„ค์ •ํ•œ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

javascript
const promise = fetch(url [, options]);

fetch ํ•จ์ˆ˜๋Š” XMLHttpRequest ๊ฐ์ฒด๋ณด๋‹ค ์‚ฌ์šฉ๋ฒ•์ด ๊ฐ„๋‹จํ•˜๊ณ  ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์ฝœ๋ฐฑ ํŒจํ„ด์˜ ๋‹จ์ ์—์„œ ์ž์œ ๋กญ๋‹ค.

Response ๊ฐ์ฒด

HTTP ์‘๋‹ต์„ ๋‚˜ํƒ€๋‚ด๋Š” Response ๊ฐ์ฒด๋ฅผ ๋ž˜ํ•‘ํ•œ Promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋‹ค์–‘ํ•œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

Response ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๋Š” ๊ธฐ๋ณธ์ ์ธ ํ”„๋กœํผํ‹ฐ

  • Response.status : HTTP Status ์ฝ”๋“œ, HTTP ์ƒํƒœ/์‘๋‹ต ์ฝ”๋“œ (๊ธฐ๋ณธ๊ฐ’ 200)
  • Response.statusText : HTTP Status ์ฝ”๋“œ์˜ ๋ฉ”์„œ๋“œ์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด (๊ธฐ๋ณธ๊ฐ’ "OK")
  • Response.ok : HTTP Status ์ฝ”๋“œ๊ฐ€ 200์—์„œ 299์ค‘ ํ•˜๋‚˜์ž„์„ ์ฒดํฌํ•˜๋Š” ๊ฐ’ (Boolean ๋ฐ˜ํ™˜)

Response.prototype์—๋Š” Response ๊ฐ์ฒด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” HTTP ์‘๋‹ต ๋ชธ์ฒด(body)๋ฅผ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, fetch ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๋ž˜ํ•‘ํ•˜๊ณ  ์žˆ๋Š” HTTP ์‘๋‹ต ๋ชธ์ฒด๋ฅผ ์ทจ๋“ํ•˜๋ ค๋ฉด Response.prototype.json ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

Response.prototype.json ๋ฉ”์†Œ๋“œ

Response ๊ฐ์ฒด์—์„œ HTTP ์‘๋‹ต ๋ชธ์ฒด(response.body)๋ฅผ ์ทจ๋“ํ•˜์—ฌ ์—ญ์ง๋ ฌํ™” ํ•œ๋‹ค.

Request Option ์ ์šฉ

fetch ํ•จ์ˆ˜์— ์š”์ฒญ ์˜ต์…˜์„ ์„ค์ •ํ•ด ๋‹ค์–‘ํ•œ ์š”์ฒญ์„ ์ „์†กํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜: HTTP ์š”์ฒญ์„ ์ „์†กํ•  URL
  • ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜: HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ, HTTP ์š”์ฒญ ํ—ค๋”, ํŽ˜์ด๋กœ๋“œ ๋“ฑ์„ ์„ค์ •ํ•œ ๊ฐ์ฒด
javascript
const request = fetch(
'https:// ...', // HTTP ์š”์ฒญ์„ ์ „์†กํ•  URL
{
method: 'POST', // HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ
headers: { 'content-Type': 'application/json' }, // HTTP ์š”์ฒญ ํ—ค๋”
body: JSON.stringify(payload) // ํŽ˜์ด๋กœ๋“œ
});
javascript
// ์š”์ฒญ ์„ค์ • ๊ฐ์ฒด, *ํ‘œ์‹œ๋Š” ๊ธฐ๋ณธ ์˜ต์…˜
{
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, cors, *same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json',
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrer: 'no-referrer', // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
}

fetch ์‚ฌ์šฉ ์˜ˆ์ œ

javascript
const request = {
get(url) {
return fetch(url);
},
post(url, payload) {
return fetch(url, {
method: 'POST',
headers: { 'content-Type': 'application/json' },
body: JSON.stringify(payload)
});
},
patch(url, payload) {
return fetch(url, {
method: 'PATCH',
headers: { 'content-Type': 'application/json' },
body: JSON.stringify(payload)
});
},
delete(url) {
return fetch(url, { method: 'DELETE' });
}
};
request.get('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(todos => console.log(todos))
.catch(err => console.error(err));
// {userId: 1, id: 1, title: "delectus aut autem", completed: false}
request.post('https://jsonplaceholder.typicode.com/todos', {
userId: 1,
title: 'JavaScript',
completed: false
})
.then(response => response.json())
.then(todos => console.log(todos))
.catch(err => console.error(err));
// {userId: 1, title: "JavaScript", completed: false, id: 201}
request.patch('https://jsonplaceholder.typicode.com/todos/1', {
completed: true
})
.then(response => response.json())
.then(todos => console.log(todos))
.catch(err => console.error(err));
// {userId: 1, id: 1, title: "delectus aut autem", completed: true}
request.delete('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(todos => console.log(todos))
.catch(err => console.error(err));
// {}

fetch ํ•จ์ˆ˜๋Š” ๋น„๊ต์  ์ตœ๊ทผ์— ์ถ”๊ฐ€๋œ Web API๋กœ์„œ ์ธํ„ฐ๋„ท ์ต์Šคํ”Œ๋กœ์–ด๋ฅผ ์ œ์™ธํ•œ ๋Œ€๋ถ€๋ถ„์˜ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

reference

Promise - JavaScript | MDN

Promise | PoiemaWeb

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Promise ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ธฐ โ€ข Captain Pangyo

ํ”„๋ผ๋ฏธ์Šค | JavaScript info

โญ๏ธ๐ŸŽ€ JavaScript Visualized: Promises & Async/Await

logo
Things I've Learned