get-wait-for-all-promise.js (1723B)
1 // |reftest| skip-if(!xulRuntime.shell) -- needs getSelfHostedValue and drainJobQueue 2 3 function onResolved(val) { 4 result = 'resolved with ' + val; 5 } 6 7 function onRejected(val) { 8 result = 'rejected with ' + val; 9 } 10 11 // Replacing `Promise#then` shouldn't affect getWaitForAllPromise. 12 let originalThen = Promise.prototype.then; 13 Promise.prototype.then = 1; 14 15 // Replacing Promise[@@species] shouldn't affect getWaitForAllPromise. 16 Object.defineProperty(Promise, Symbol.species, { get: function(){} }); 17 18 // Replacing `Promise` shouldn't affect getWaitForAllPromise. 19 let PromiseCtor = Promise; 20 Promise = {}; 21 22 // Replacing Array[@@iterator] shouldn't affect getWaitForAllPromise. 23 Array.prototype[Symbol.iterator] = function(){}; 24 25 let resolveFunctions = []; 26 let rejectFunctions = []; 27 let promises = []; 28 for (let i = 0; i < 3; i++) { 29 let p = new PromiseCtor(function(res_, rej_) { 30 resolveFunctions.push(res_); 31 rejectFunctions.push(rej_); 32 }); 33 promises.push(p); 34 } 35 36 let allPromise = getWaitForAllPromise(promises); 37 let then = originalThen.call(allPromise, onResolved, onRejected); 38 39 resolveFunctions.forEach((fun, i)=>fun(i)); 40 drainJobQueue(); 41 42 assertEq(result, 'resolved with 0,1,2'); 43 44 // Empty lists result in a promise resolved with an empty array. 45 result = undefined; 46 originalThen.call(getWaitForAllPromise([]), v=>(result = v)); 47 drainJobQueue(); 48 assertEq(result instanceof Array, true); 49 assertEq(result.length, 0); 50 51 //Empty lists result in a promise resolved with an empty array. 52 result = undefined; 53 originalThen.call(getWaitForAllPromise([]), v=>(result = v)); 54 55 drainJobQueue(); 56 57 assertEq(result instanceof Array, true); 58 assertEq(result.length, 0); 59 60 this.reportCompare && reportCompare(true,true);