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