resolve-from-same-thenable.js (2363B)
1 // Copyright (C) 2019 Leo Balter. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 esid: sec-performpromiseallsettled 6 description: > 7 Cannot tamper remainingElementsCount when Promise.allSettled resolve element function is called multiple times. 8 info: | 9 Runtime Semantics: PerformPromiseAllSettled ( iteratorRecord, constructor, resultCapability ) 10 11 4. Let remainingElementsCount be a new Record { [[Value]]: 1 }. 12 ... 13 6.d ... 14 ii. Set remainingElementsCount.[[value]] to remainingElementsCount.[[value]] − 1. 15 iii. If remainingElementsCount.[[value]] is 0, then 16 1. Let valuesArray be CreateArrayFromList(values). 17 2. Perform ? Call(resultCapability.[[Resolve]], undefined, « valuesArray »). 18 ... 19 20 Promise.allSettled Resolve Element Functions 21 22 2. Let alreadyCalled be F.[[AlreadyCalled]]. 23 3. If alreadyCalled.[[Value]] is true, return undefined. 24 4. Set alreadyCalled.[[Value]] to true. 25 ... 26 includes: [promiseHelper.js] 27 features: [Promise.allSettled] 28 ---*/ 29 30 var callCount = 0; 31 32 function Constructor(executor) { 33 function resolve(values) { 34 callCount += 1; 35 checkSettledPromises(values, [ 36 { 37 status: 'fulfilled', 38 value: 'p1-fulfill' 39 }, 40 { 41 status: 'fulfilled', 42 value: 'p2-fulfill' 43 }, 44 { 45 status: 'fulfilled', 46 value: 'p3-fulfill' 47 } 48 ], 'values'); 49 } 50 executor(resolve, Test262Error.thrower); 51 } 52 Constructor.resolve = function(v) { 53 return v; 54 }; 55 56 var p1OnFulfilled, p2OnFulfilled, p3OnFulfilled; 57 58 var p1 = { 59 then(onFulfilled, onRejected) { 60 p1OnFulfilled = onFulfilled; 61 } 62 }; 63 var p2 = { 64 then(onFulfilled, onRejected) { 65 p2OnFulfilled = onFulfilled; 66 } 67 }; 68 var p3 = { 69 then(onFulfilled, onRejected) { 70 p3OnFulfilled = onFulfilled; 71 } 72 }; 73 74 assert.sameValue(callCount, 0, 'callCount before call to allSettled()'); 75 76 Promise.allSettled.call(Constructor, [p1, p2, p3]); 77 78 assert.sameValue(callCount, 0, 'callCount after call to allSettled()'); 79 80 p1OnFulfilled('p1-fulfill'); 81 p1OnFulfilled('p1-fulfill-unexpected-1'); 82 p1OnFulfilled('p1-fulfill-unexpected-2'); 83 84 assert.sameValue(callCount, 0, 'callCount after resolving p1'); 85 86 p2OnFulfilled('p2-fulfill'); 87 p3OnFulfilled('p3-fulfill'); 88 89 assert.sameValue(callCount, 1, 'callCount after resolving all elements'); 90 91 reportCompare(0, 0);