Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Promise.resolve()

この記事は編集レビューを必要としています。ぜひご協力ください

この翻訳は不完全です。英語から この記事を翻訳 してください。

これは Harmony(ECMAScript 6) 提案の一部であり、実験段階の技術です。
この技術の仕様は安定していません。ブラウザ互換性の一覧表を確認してください。またこれらの構文や動作は、仕様変更などにより、新しいバージョンのブラウザでは変更される可能性があるという点に注意してください。

概要

引数で与えられた値で完了されたPromise オブジェクトを返します。値が成功なら (すなわちthen メソッドを持っている)、返されるプロミスは成功に従い、その最終的な状態を採用します。さもなければ、返されるプロミスはその値で失敗にされます。

構文

Promise.resolve(value);
Promise.resolve(promise);
Promise.resolve(thenable);

引数

value
Promiseによって完了される引数。Promiseまたは完了するthen

説明

静的なPromise.resolve 関数は完了するPromiseを返します。

静的なPromise.resolve メソッドを使う

Promise.resolve("Success").then(function(value) {
  console.log(value); // "Success"
}, function(value) {
  // not called
});

配列を完了する

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
});

thenablesを完了しエラーをスローする

// Resolving a thenable object
var p1 = Promise.resolve({ 
  then: function(onFulfill, onReject) { onFulfill("fulfilled!"); }
});
console.log(p1 instanceof Promise) // true, object casted to a Promise

p1.then(function(v) {
    console.log(v); // "fulfilled!"
  }, function(e) {
    // not called
});

// Thenable throws before callback
// Promise rejects
var thenable = { then: function(resolve) {
  throw new TypeError("Throwing");
  resolve("Resolving");
}};

var p2 = Promise.resolve(thenable);
p2.then(function(v) {
  // not called
}, function(e) {
  console.log(e); // TypeError: Throwing
});

// Thenable throws after callback
// Promise resolves
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) {
  // not called
});

仕様

仕様 状況 コメント
domenic/promises-unwrapping Draft Standardization work is taking place here.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Promise.resolve' in that specification.
Standard Initial definition in an ECMA standard.

ブラウザ実装状況

機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
基本サポート 32 [2] 24.0 (24.0) as Future
25.0 (25.0) as Promise behind a flag[1]
29.0 (29.0) by default
未サポート 19 7.1
機能 Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
基本サポート 未サポート 24.0 (24.0) as Future
25.0 (25.0) as Promise behind a flag[1]
29.0 (29.0) by default
未サポート 未サポート iOS 8 32

[1] Gecko 24では、PromiseFutureの初期名称で実験的に実装しています。Gecko 25で最終的な名称にリネームされました。しかし、dom.promise.enabledフラグによってデフォルトで無効になっています。Bug 918806では、Gecko 29でデフォルトでプロミスを有効にしました。

[2] Chrome/v8 も非標準メソッドPromise.accept()をサポートします。しかしながら、この標準の Promise.resolve() メソッドを代わりに使うことが推奨されています。というのも、acceptメソッドが将来取り除かれる予定であるからです(issue 3238)。

関連情報

ドキュメントのタグと貢献者

 このページの貢献者: shide55
 最終更新者: shide55,