Promise.resolve(value) 메서드는 주어진 값으로 결정(resolve)되는 Promise.then 객체를 반환합니다. 그 값이 thenable(즉 "then" 메서드가 있음)인 경우, 반환된 프로미스는 그 thenable을 "따르며", 그 최종 상태를 취합니다; 그렇지 않으면 반환된 프로미스는 그 값으로 이행(fulfill)됩니다.
구문
Promise.resolve(value); Promise.resolve(promise); Promise.resolve(thenable);
매개변수
- value
- 이
Promise에 의해 결정되는 인수.Promise또는 결정하는 thenable도 될 수 있습니다.
설명
정적 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(true);
var cast = Promise.resolve(original);
cast.then(function(v) {
console.log(v); // true
});
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이 콜백 전에 오류 던짐
// 프로미스 거부
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이 콜백 후에 오류 던짐
// 프로미스 결정
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) {
// 호출되지 않음
});
스펙
브라우저 호환성
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 32 [1] | 29.0 (29.0) | No support | 19 | 7.1 |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|
| Basic support | No support | 29.0 (29.0) | No support | No support | 8 | 32 |
[1] Chrome/v8은 비표준 메서드 Promise.accept()도 지원합니다. 그러나, accept 메서드는 머지 않아 제거될 것이기에 대신 이 표준 Promise.resolve() 메서드 사용이 권장됩니다 (이슈 3238).