1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions 6 // are met: 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright 10 // notice, this list of conditions and the following disclaimer in the 11 // documentation and/or other materials provided with the distribution. 12 // 13 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24 // Flags: --harmony 25 'use strict'; 26 description('Test Promise.race'); 27 28 var result; 29 30 var p1 = new Promise(function(resolve) { resolve('p1'); }); 31 var p2 = new Promise(function(resolve) { resolve('p2'); }); 32 var p3 = new Promise(function(resolve) { resolve('p3'); }); 33 var p4 = new Promise(function() {}); 34 var p5 = new Promise(function() {}); 35 var p6 = new Promise(function(_, reject) { reject('p6'); }); 36 var p7 = new Promise(function(_, reject) { reject('p7'); }); 37 var p8 = new Promise(function(_, reject) { reject('p8'); }); 38 var p9 = new Promise(function(resolve) { resolve(p2); }); 39 40 Promise.race([p4, p5]).then(function(localResult) { 41 testFailed('Promise.race([p4, p5]) is fulfilled.'); 42 }, function() { 43 testFailed('Promise.race([p4, p5]) is rejected.'); 44 }); 45 46 // If the argument is an empty array, the result promise won't be fulfilled. 47 Promise.race([]).then(function(localResult) { 48 testFailed('Promise.race([]) is fulfilled.'); 49 }, function() { 50 testFailed('Promise.race([]) is rejected.'); 51 }); 52 53 Promise.race().then(function(localResult) { 54 testFailed('Promise.race() is fulfilled.'); 55 }, function() { 56 testPassed('Promise.race() is rejected.'); 57 }).then(function() { 58 return Promise.race({}).then(function(localResult) { 59 testFailed('Promise.race({}) is fulfilled.'); 60 }, function() { 61 testPassed('Promise.race({}) is rejected.'); 62 }); 63 }).then(function() { 64 return Promise.race([p4, p1, p6]).then(function(localResult) { 65 testPassed('Promise.race([p4, p1, p6]) is fulfilled.'); 66 result = localResult; 67 shouldBeEqualToString('result', 'p1'); 68 }, function() { 69 testFailed('Promise.race([p4, p1, p6]) is rejected.'); 70 }); 71 }).then(function() { 72 return Promise.race([p4, p6, p1]).then(function(localResult) { 73 testFailed('Promise.race([p4, p6, p1]) is fulfilled.'); 74 }, function(localResult) { 75 testPassed('Promise.race([p4, p6, p1]) is rejected.'); 76 result = localResult; 77 shouldBeEqualToString('result', 'p6'); 78 }); 79 }).then(function() { 80 return Promise.race([p9]).then(function(localResult) { 81 testPassed('Promise.race([p9]) is fulfilled.'); 82 result = localResult; 83 shouldBeEqualToString('result', 'p2'); 84 }, function() { 85 testFailed('Promise.race([p9]) is rejected.'); 86 }); 87 }).then(function() { 88 // Array hole should not be skipped. 89 return Promise.race([p4,,]).then(function(localResult) { 90 testPassed('Promise.race([p4,,]) is fulfilled.'); 91 result = localResult; 92 shouldBe('result', 'undefined'); 93 }, function() { 94 testFailed('Promise.race([p4,,]) is rejected.'); 95 }); 96 }).then(function() { 97 // Immediate value should be converted to a promise object by the 98 // ToPromise operation. 99 return Promise.race([p4,42]).then(function(localResult) { 100 testPassed('Promise.race([p4,42]) is fulfilled.'); 101 result = localResult; 102 shouldBe('result', '42'); 103 }, function() { 104 testFailed('Promise.race([p4,42]) is rejected.'); 105 }); 106 }).then(finishJSTest, finishJSTest); 107 108 shouldBe('result', 'undefined'); 109