Promise.resolve()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itโs been available across browsers since โจ2015๋ 7์โฉ.
Promise.resolve(value)
๋ฉ์๋๋ ์ฃผ์ด์ง ๊ฐ์ผ๋ก ์ดํํ๋ Promise.then
๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. ๊ทธ ๊ฐ์ด ํ๋ก๋ฏธ์ค์ธ ๊ฒฝ์ฐ, ํด๋น ํ๋ก๋ฏธ์ค๊ฐ ๋ฐํ๋ฉ๋๋ค. ๊ทธ ๊ฐ์ด thenable(์, "then" ๋ฉ์๋
๊ฐ ์์)์ธ ๊ฒฝ์ฐ, ๋ฐํ๋ ํ๋ก๋ฏธ์ค๋ ๊ทธ thenable์ "๋ฐ๋ฅด๋ฉฐ", ๊ทธ ์ต์ข
์ํ๋ฅผ ์ทจํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ๋ฐํ๋ ํ๋ก๋ฏธ์ค๋ ๊ทธ ๊ฐ์ผ๋ก ์ดํํฉ๋๋ค. ์ด ํจ์๋ ํ๋ก๋ฏธ์คํ์ ๊ฐ์ฒด(๋ฌด์ธ๊ฐ๋ฅผ ๊ฒฐ์ ํ๋ ํ๋ก๋ฏธ์ค๋ฅผ ๊ฒฐ์ ํ๋ ํ๋ก๋ฏธ์ค ๋ฑ)์ ์ค์ฒฉ๋ ๋ ์ด์ด๋ฅผ ๋จ์ผ ๋ ์ด์ด๋ก ํผ์นฉ๋๋ค.
๊ฒฝ๊ณ : ์ฃผ์: ์ค์ค๋ก๋ฅผ ๊ฒฐ์ ํ๋ thenable ์์
Promise.resolve
๋ฅผ ํธ์ถํ๋ฉด ์๋ฉ๋๋ค. ์ด๋ ๋ฌดํํ ์ค์ฒฉ๋ ํ๋ก๋ฏธ์ค๋ฅผ ํผ์น๋ ค๊ณ ํ๋ฏ๋ก ๋ฌดํ ์ฌ๊ท๋ฅผ ์ ๋ฐํ ๊ฒ์ ๋๋ค. Angular ์์async
Pipe ๋ฅผ ํจ๊ป ์ฌ์ฉํ ์์ ์ ๋๋ค. ์์ธํ ๋ด์ฉ์ ์ฌ๊ธฐ์์ ํ์ธํ์ธ์.
์๋ํด ๋ณด๊ธฐ
const promise1 = Promise.resolve(123);
promise1.then((value) => {
console.log(value);
// Expected output: 123
});
๊ตฌ๋ฌธ
Promise.resolve(value);
ํ๋ผ๋ฏธํฐ
- value
-
์ด
Promise
์ ์ํด ๊ฒฐ์ ๋๋ ์ธ์.Promise
๋๋ ์ดํํ thenable ์ผ์๋ ์์ต๋๋ค.
๋ฐํ ๊ฐ
์ฃผ์ด์ง ๊ฐ์ผ๋ก ์ดํ๋
Promise
๋๋ ๊ฐ์ด promise ๊ฐ์ฒด์ธ ๊ฒฝ์ฐ, ๊ฐ์ผ๋ก ์ ๋ฌ๋ promise.
์ค๋ช
์ ์ Promise.resolve
ํจ์๋ ์ดํ๋ Promise
๋ฅผ ๋ฐํํฉ๋๋ค.
์์
์ ์ Promise.resolve
๋ฉ์๋ ์ฌ์ฉ
Promise.resolve("Success").then(
function (value) {
console.log(value); // "Success"
},
function (value) {
// ํธ์ถ๋์ง ์์
},
);
๋ฐฐ์ด ์ดํ
var p = Promise.resolve([1, 2, 3]);
p.then(function (v) {
console.log(v[0]); // 1
});
๋ ๋ค๋ฅธ Promise
์ดํ
var original = Promise.resolve(33);
var cast = Promise.resolve(original);
cast.then(function (value) {
console.log("value: " + value);
});
console.log("original === cast ? " + (original === cast));
// ๋ก๊ทธ ์์:
// original === cast ? true
// value: 33
๋ก๊ทธ์ ์์๊ฐ ๋ฐ๋์ธ ์ด์ ๋ then
ํธ๋ค๋ฌ๊ฐ ๋น๋๊ธฐ๋ก ํธ์ถ๋๊ธฐ ๋๋ฌธ์
๋๋ค. ์ฌ๊ธฐ์์ then
์ด ๋์ํ๋ ๋ฐฉ์์ ๋ํด ํ์ธํ์ธ์.
thenable ์ดํ ๋ฐ ์ค๋ฅ ๋ฐ์
// thenable ๊ฐ์ฒด ์ดํ
var p1 = Promise.resolve({
then: function (onFulfill, onReject) {
onFulfill("fulfilled!");
},
});
console.log(p1 instanceof Promise); // true, ๊ฐ์ฒด๋ Promise ๋ก ๋ณํ๋จ
p1.then(
function (v) {
console.log(v); // "fulfilled!"
},
function (e) {
// ํธ์ถ๋์ง ์์
},
);
// thenable ์ด ์ฝ๋ฐฑ ์ด์ ์ ์ค๋ฅ๋ฅผ throw
// Promise ๊ฑฐ๋ถ
var thenable = {
then: function (resolve) {
throw new TypeError("Throwing");
resolve("Resolving");
},
};
var p2 = Promise.resolve(thenable);
p2.then(
function (v) {
// ํธ์ถ๋์ง ์์
},
function (e) {
console.log(e); // TypeError: Throwing
},
);
// thenable ์ด ์ฝ๋ฐฑ ์ดํ์ ์ค๋ฅ๋ฅผ throw
// Promise ์ดํ
var thenable = {
then: function (resolve) {
resolve("Resolving");
throw new TypeError("Throwing");
},
};
var p3 = Promise.resolve(thenable);
p3.then(
function (v) {
console.log(v); // "Resolving"
},
function (e) {
// ํธ์ถ๋์ง ์์
},
);
๋ช ์ธ
Specification |
---|
ECMAScriptยฎ 2026 Language Specification # sec-promise.resolve |
๋ธ๋ผ์ฐ์ ํธํ์ฑ
Loadingโฆ