reject-from-same-thenable.js (1628B)
1 // Copyright (C) 2020 Rick Waldron. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 esid: sec-performpromiserace 6 description: > 7 Promise.race does not prevent resolve from being called multiple times. 8 info: | 9 PerformPromiseRace 10 11 Repeat, 12 Let next be IteratorStep(iteratorRecord). 13 If next is an abrupt completion, set iteratorRecord.[[Done]] to true. 14 ReturnIfAbrupt(next). 15 If next is false, then 16 Set iteratorRecord.[[Done]] to true. 17 Return resultCapability.[[Promise]]. 18 Let nextValue be IteratorValue(next). 19 If nextValue is an abrupt completion, set iteratorRecord.[[Done]] to true. 20 ReturnIfAbrupt(nextValue). 21 Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »). 22 Perform ? Invoke(nextPromise, "then", « resultCapability.[[Resolve]], resultCapability.[[Reject]] »). 23 24 includes: [promiseHelper.js] 25 ---*/ 26 27 let callCount = 0; 28 let sequence = []; 29 30 function Constructor(executor) { 31 function reject(value) { 32 callCount += 1; 33 sequence.push(value); 34 } 35 executor(() => { 36 throw new Test262Error(); 37 }, reject); 38 } 39 Constructor.resolve = function(v) { 40 return v; 41 }; 42 43 let pReject; 44 45 let a = { 46 then(_, rejecter) { 47 pReject = rejecter; 48 } 49 }; 50 51 assert.sameValue(callCount, 0, 'callCount before call to race()'); 52 53 Promise.race.call(Constructor, [a]); 54 55 assert.sameValue(callCount, 0, 'callCount after call to race()'); 56 57 pReject(1); 58 pReject(2); 59 pReject(3); 60 61 assert.sameValue(callCount, 3, 'callCount after resolving a'); 62 assert.sameValue(sequence.length, 3); 63 checkSequence(sequence); 64 65 reportCompare(0, 0);