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.race()

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

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

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

概要

反復のプロミスの一つが成功したり失敗したりしてすぐに値や理由とともにプロミスを返します。

構文

Promise.race(iterable);

引数

iterable
Arrayのような反復オブジェクト。iterableを確かめて下さい。

説明

race 関数は最初に渡されたプロミスと同様に解決されたPromiseを返します。 解決か拒否の早い方をします。

Promise.race を使う– setTimeoutと使う例

var p1 = new Promise(function(resolve, reject) { 
    setTimeout(resolve, 500, "one"); 
});
var p2 = new Promise(function(resolve, reject) { 
    setTimeout(resolve, 100, "two"); 
});

Promise.race([p1, p2]).then(function(value) {
  console.log(value); // "two"
  // Both resolve, but p2 is faster
});

var p3 = new Promise(function(resolve, reject) { 
    setTimeout(resolve, 100, "three");
});
var p4 = new Promise(function(resolve, reject) { 
    setTimeout(reject, 500, "four"); 
});

Promise.race([p3, p4]).then(function(value) {
  console.log(value); // "three"
  // p3 is faster, so it resolves               
}, function(reason) {
  // Not called
});

var p5 = new Promise(function(resolve, reject) { 
    setTimeout(resolve, 500, "five"); 
});
var p6 = new Promise(function(resolve, reject) { 
    setTimeout(reject, 100, "six");
});

Promise.race([p5, p6]).then(function(value) {
  // Not called              
}, function(reason) {
  console.log(reason); // "six"
  // p6 is faster, so it rejects
});

仕様

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

ブラウザ実装状況

機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
基本サポート 32 29.0 (29.0) 未サポート 19 7.1
機能 Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
基本サポート 未サポート 29.0 (29.0) 未サポート 未サポート iOS 8 32

関連情報

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

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